Implement two stacks in an array | Practice | GeeksforGeeks

Your task is to implement  2 stacks in one array efficiently.
 
Example 1:
Input:
push1(2)
push1(3)
push2(4)
pop1()
pop2()
pop2()

Output:
3 4 -1

Explanation:
push1(2) the stack1 will be {2}
push1(3) the stack1 will be {2,3}
push2(4) the stack2 will be {4}
pop1()   the poped element will be 3 
from stack1 and stack1 will be {2}
pop2()   the poped element will be 4 
from stack2 and now stack2 is empty
pop2()   the stack2 is now empty hence -1 .
 
Your Task:
You don't need to read input or print anything. You are required to complete the 4 methods push1, push2 which takes one argument an integer 'x' to be pushed into stack one and two and pop1, pop2 which returns the integer poped out from stack one and two. If no integer is present in the array return -1.
 
Expected Time Complexity: O(1) for all the four methods.
Expected Auxiliary Space: O(1) for all the four methods.
 
Constraints:
1 <= Number of queries <= 100
1 <= value in the stack <= 100
The sum of elements in both the stacks < size of the given array
0
jainmuskan5652 weeks ago
void twoStacks :: push1(int x){   if(top1+1<top2){       top1++;       arr[top1]=x;   }}  //Function to push an integer into the stack2.void twoStacks ::push2(int x){   if(top2-1> top1){       top2--;       arr[top2]=x;   }}  //Function to remove an element from top of the stack1.int twoStacks ::pop1(){   if(top1==-1){       return -1;   }   int tmp= arr[top1];   top1--;   return tmp;}
//Function to remove an element from top of the stack2.int twoStacks :: pop2(){   if(top2==-1 || top2== size){       return -1;   }   int tmp= arr[top2];   top2++;   return  tmp;} 
0
hharshit81182 weeks ago
Time Complexity : O(1) for all methods
void twoStacks :: push1(int x){   if(top1+1 < top2){       ++top1;       arr[top1] = x;   }}  //Function to push an integer into the stack2.void twoStacks ::push2(int x){ if(top1 < top2-1){       --top2;       arr[top2] = x;   }}  //Function to remove an element from top of the stack1.int twoStacks ::pop1(){   if(top1 == -1){       return -1;   }   int x = arr[top1];   top1--;   return x;}
//Function to remove an element from top of the stack2.int twoStacks :: pop2(){     if(top2 == size){       return -1;   }
+1
injamamulmzp2315012 weeks ago
Easy Solution in Python
def push1(a,x):   #code here   i = 0   while a[i]!=-1:       i+=1   a[i] = x#Function to push an integer into the stack2.def push2(a,x):   #code here   a.append(x)   #Function to remove an element from top of the stack1.    def pop1(a):   #code here   i = 0   while a[i]!=-1:       i+=1   if i>0:       x = a[i-1]       a[i-1]=-1       return x   return -1   #Function to remove an element from top of the stack2.    def pop2(a):   #code here   if a[-1]!=-1:       return a.pop()   return -1   
+1
jagannath23sahoo2 weeks ago
C++ Solution
TC: O(1)
Aux SC: O(1)
//Function to push an integer into the stack1.
void twoStacks :: push1(int x)
{
    if(top1 < top2-1)
    {
        arr[++top1] = x;
    }
    
}
   
//Function to push an integer into the stack2.
void twoStacks ::push2(int x)
{
    if(top1 < top2-1)
    {
        arr[--top2] = x;
    }
}
   
//Function to remove an element from top of the stack1.
int twoStacks ::pop1()
{
     int x;
    if(top1 >= 0)
    {
       x = arr[top1];
        top1--;
    }
    else{
        return -1;
    }
    return x;
}

//Function to remove an element from top of the stack2.
int twoStacks :: pop2()
{
    int x;
    if(top2 < size)
    {
       x = arr[top2];
        top2++;
    }
    else{
        return -1;
    }
    return x;
}
+1
22kavachdheer2 weeks ago
//Function to push an integer into the stack1.void twoStacks :: push1(int x){  if(top1 < top2-1){      top1++;      arr[top1]=x;  }}  //Function to push an integer into the stack2.void twoStacks ::push2(int x){   if(top1 < top2-1){      top2--;      arr[top2]=x;  }}  //Function to remove an element from top of the stack1.int twoStacks ::pop1(){   if(top1 >=0){       int x=arr[top1];       top1--;       return x;   }   else       return(-1);}
//Function to remove an element from top of the stack2.int twoStacks :: pop2(){   if(top2 < size){       int x=arr[top2];       top2++;       return x;   }   else       return(-1);}
 
+1
himanshukug19cs3 weeks ago
java soution
 void push1(int x, TwoStack sq)   {       sq.arr[sq.top1+1]=x;       sq.top1=sq.top1+1;   }
   //Function to push an integer into the stack2.   void push2(int x, TwoStack sq)   {        sq.arr[sq.top2-1]=x;       sq.top2=sq.top2-1;
   }
   //Function to remove an element from top of the stack1.   int pop1(TwoStack sq)   {  if(sq.top1!=-1){       int temp=sq.arr[sq.top1];       sq.arr[sq.top1]=0;       sq.top1=sq.top1-1;       return temp;   }   return -1;   }
   //Function to remove an element from top of the stack2.   int pop2(TwoStack sq)   {if(sq.top2!=100){        int temp=sq.arr[sq.top2];       sq.arr[sq.top2]=0;       sq.top2=sq.top2+1;       return temp;   }       return -1;   }
0
maneshram20Premium4 weeks ago
C++ –  BEST SOLUTION : EASY UNDERSTANDING 
void twoStacks :: push1(int x)
{
    if(top2-top1>1){
        arr[++top1] = x;
    }
}
   
//Function to push an integer into the stack2.
void twoStacks ::push2(int x)
{
    if(top2-top1>1){
        arr[--top2] = x;
    }
}
   
//Function to remove an element from top of the stack1.
int twoStacks ::pop1()
{
    if(top1>=0){
        int n = arr[top1];
        top1--;
        return n;
    }
    else return -1;
}

//Function to remove an element from top of the stack2.
int twoStacks :: pop2()
{
    if(top2<size){
        int n = arr[top2];
        top2++;
        return n;
    }
    else return -1;
}

0
piyush104 weeks ago
C#public void push1(int x, TwoStack sq)       {           sq.arr[++sq.top1] = x;       }          //Function to push an integer into the stack2.       public void push2(int x, TwoStack sq)       {           sq.arr[--sq.top2] = x;       }          //Function to remove an element from top of the stack1.       public int pop1(TwoStack sq)       {           if(sq.top1 < 0) return -1;           return sq.arr[sq.top1--];       }          //Function to remove an element from top of the stack2.       public int pop2(TwoStack sq)       {           if(sq.top2 >= sq.size) return -1;           return sq.arr[sq.top2++];       }
0
tarunkanade1 month ago
0.4/1.6
class Stacks
{
    //Function to push an integer into the stack1.
    void push1(int x, TwoStack sq)
    {
        if(sq.top1 < sq.top2-1){
            sq.arr[++sq.top1] = x;
        }
    }

    //Function to push an integer into the stack2.
    void push2(int x, TwoStack sq)
    {
        if(sq.top1 < sq.top2-1){
            sq.arr[--sq.top2] = x;
        }
    }

    //Function to remove an element from top of the stack1.
    int pop1(TwoStack sq)
    {
        if(sq.top1 != -1){
            int res = sq.arr[sq.top1];
            sq.top1--;
            return res;
        }
        
        return -1;
    }

    //Function to remove an element from top of the stack2.
    int pop2(TwoStack sq)
    {
        if(sq.top2 != sq.size){
            int res = sq.arr[sq.top2];
            sq.top2++;
            return res;
        }
        
        return -1;
    }
}
0
amarrajsmart1971 month ago
Easy C++ Solution .Time Taken =0.0 /2.0
time complexity =O(1) in constant time.
set Top1 At the starting of the array and top2 at the end of the array.
 
void twoStacks :: push1(int x){    arr[++top1]=x;    }  //Function to push an integer into the stack2.void twoStacks ::push2(int x){   arr[--top2]=x;}  //Function to remove an element from top of the stack1.int twoStacks ::pop1(){    if(top1==-1)   return -1;   return arr[top1--];}
//Function to remove an element from top of the stack2.int twoStacks :: pop2(){   if(top2==size)   return -1;   return arr[top2++];}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.