Java Program to Implement Two Stacks in an Array10 Sept 2024 | 4 min read In this section, we will create a Java program that implemnts two stacks in an array. The meaning of two stack means both the stacks should use the same array for keeping the elements. The following are a few methods that must be implemented by those two stacks. push1(int i) -> pushes i into the first stack push2(int j) -> pushes j into the second stack pop1() -> removes the top element from the first stack and then returns the element that is popped. pop2() -> removes the top element from the second stack and then returns the element that is popped. Note: Implementation of the two stacks should be space-efficient.Naive ApproachThe simple approach is to divide the given array into two halves and then use those halves as the stack. However, it may lead to non-efficient use of the memory. For example, let's assume the size of the given array is 6. Thus, each stack will have a size of 3. Also, assume that we have to push 4 elements in the first stack and 1 element in the second stack. We can easily put 1 element in the second stack. However, we can only push 3 elements in the first stack. Pushing the 4th element will cause overflow. Ideally, it should not happen as there are some vacant regions (space for 2 elements is still empty) in the second stack, which can be utilized. However, we can not use it for the first stack as it is already assigned to the second. Therefore, we can say that the simple approach implementation of the two stacks will not be space-efficient. To make the implementation space efficient, it is required to allocate the space of the two stacks as per the need. The concept is to begin the two stacks from the two extreme sides of the input arr[]. Stack 1 begins from the leftmost side, which means the first element in stack 1 is pushed at the index 0. Stack 2 begins from the rightmost side. The first element in stack 2 is pushed at the index size - 1. Both the stacks shrink or grow in the opposite direction. For checking the overflow, one needs to look after the space between elements that are residing at the top of both the stacks. The following program shows the same. FileName: TwoStacksArray.java Output: The popped element from the stack 1 is: 191 The popped element from the stack 2 is: 40 Complexity Analysis: For the push and pop operation, constant time is taken. Hence, for the operations, the time complexity is O(1). Next TopicJava Snippet |
In the world of computer science and programming, matrix manipulation is a fundamental concept with applications in various domains, such as graphics, image processing, and scientific computations. One interesting and common matrix manipulation is the horizontal flip. In this section, we will discuss the horizontal...
5 min read
Difference Between Java and PHP Java and PHP are both the two most popular programming languages. There are so many differences and similarities between both of them. Let's understand both of them one by one, and after that, we understand the similarities and differences between them. Java Java is...
4 min read
Correctly nesting brackets is a common problem in computer science, particularly with mathematical equations, interpreters, and compilers. If the sequence of the appropriate opening and closing parenthesis is preserved, a set of parentheses is considered "properly nested." Problem Statement Given a string containing only the characters ( and...
7 min read
Java timestamps record the creation, modification, and update times of program elements as well as actions. They are widely used in financial applications, scientific research, and other domains where precise time data is required. Java Timestamps may be used to compute, compare, and identify the time...
4 min read
The Delegation Event model is defined to handle events in GUI programming languages. The GUI stands for Graphical User Interface, where a user graphically/visually interacts with the system. The GUI programming is inherently event-driven; whenever a user initiates an activity such as a mouse activity, clicks, scrolling,...
7 min read
Java programming supports different data structures like array, linked list, stack, queue, etc. Each data structure has operations such as insertion, deletion, searching an element. And to implement these operations Java programming provides built in classes and methods. In this section, we will understand the pop...
4 min read
In this section, we will discuss how to reverse a linked list in Java. Reversing a linked list is one of the most prominent topics that is asked in interviews. The task is to reverse a linked list, provided the head or the first node of...
10 min read
LinkedHashMap is a pre-defined class in Java programming that can be extended from HashMap. It gives the hash table a consistent order of iterations. The java.util package contains LinkedHashMap, which is used to maintain double-linked lists. Syntax: LinkedHashMap<K, V> map = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder); Parameters: K:...
3 min read
abstract Keyword in Java The Java abstract keyword is a non-access modifier used with classes and methods to achieve abstraction. Purpose of abstract Keyword The abstract keyword facilitates abstraction by allowing us to define a blueprint or a contract for classes without providing complete implementation details. It promotes...
5 min read
? We can check whether an integer exists in a range in Java using conditional statements with the lower and upper bounds of the range. To check whether an integer exists in a range, we can use the following steps: Define the range (start and end) values. Compare the integer...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India