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 |
Literals in Java
In Java, literal is a notation that represents a fixed value in the source code. In lexical analysis, literals of a given type are generally known as tokens. In this section, we will discuss the term literals in Java. Literals In Java, literals are the constant values...
6 min read
Java Program to Find the Minimal Nucleotide from A Range of Sequence DNA
It is often the case in computational biology to find the global minimal nucleotide in a DNA sequence, as well as in a given range. DNA sequences consist of four nucleotides. The four bases that have been represented by letters are adenine (A), cytosine (C), guanine...
6 min read
How to Print Pattern in Java
Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have a...
13 min read
Converting Long to Date in JAVA
Converting Long to Date in Java In this article, we are about to learn what are Long and Date in Java and their implementation in the Java Programming language. We are also going to discuss in-depth how to convert a Long value into a Date value in...
8 min read
Java Program to Cyclically Permute the Elements of an Array
Cyclic permutation of array elements is a fundamental technique in computer science, used to rotate elements within a fixed-size array such that each element shifts one position to the right, with the last element wrapping around to the first position. This operation is essential in various...
4 min read
Java Static Type Vs Dynamic Type
Java Static Type Vs Dynamic Type Java is a strongly-typed language that categorizes variables, expressions, and objects into static types. However, Java also supports dynamic typing through the use of its object-oriented features. In this section, we will explore the concepts of static and dynamic typing in...
5 min read
Exam Seating Arrangement in Java
Exam seating arrangement in Java involves designing a program to assign seats to students in an exam hall, ensuring fairness and adherence to specific rules, such as enting cheating by separating friends or similar roll numbers. It typically includes sorting, grid allocation, and applying constraints programmatically...
9 min read
Mirror of n-ary Tree in Java
A mirror of an N-ary tree is a transformation where the left and right subtrees of every node are swapped. This concept is similar to the mirror of a binary tree, but here, each node can have more than two children. To obtain the mirror of...
10 min read
Pernicious Number in Java
In this section, we will discuss what is pernicious number and also create Java programs to check if the given number is a pernicious number or not. The pernicious number program frequently asked in Java coding interviews and academics. Pernicious Number If the total number of 1's in...
4 min read
Stack vs Heap Java
Stack Vs Heap Java In Java, memory management is a vital process. It is managed by Java automatically. The JVM divides the memory into two parts: stack memory and heap memory. From the perspective of Java, both are important memory areas but both are used for different...
3 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