Stacks
Stacks
Stacks
38) 2
stacks in an array
https://www.codingninjas.com/codestudio/problems/two-stacks_983634?
leftPanelTab=0&campaign=YouTube_CodestudioLovebabbar5thfeb&utm_source=youtube&utm_m
edium=affiliate&utm_campaign=YouTube_CodestudioLovebabbar5thfeb
#include <bits/stdc++.h>
class TwoStack {
public:
int* arr;
int top1;
int top2;
int size;
// Initialize TwoStack.
TwoStack(int s) {
// Write your code here.
this->size = s;
top1 = -1;
top2 = s;
arr = new int[s];
}
// Push in stack 1.
void push1(int num) {
// Write your code here.
if((top2 - top1)>1){
top1++;
arr[top1]=num;
}
}
// Push in stack 2.
void push2(int num) {
// Write your code here.
if((top2 - top1)>1){
top2--;
arr[top2]=num;
}
}
_________________________________________________________________________Q. 39)
reverse a string using stack
in vs code
__________________________________________________________________________Q. 40)
Delete middle element from stack
https://www.codingninjas.com/codestudio/problems/delete-middle-element-from-
stack_985246?
leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliat
e&utm_campaign=Lovebabbarcodestudio
#include <bits/stdc++.h>
inputStack.push(num);
int count = 0;
solve(inputStack, count , N);
}
___________________________________________________________________________Q. 41
Valid Parentheses
https://www.codingninjas.com/codestudio/problems/valid-parenthesis_795104?
topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium
=affiliate&utm_campaign=Lovebabbarcodestudio
https://www.codingninjas.com/codestudio/problems/insert-an-element-at-its-bottom-
in-a-given-stack_1171166?topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0%3Fsource
%3Dyoutube&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliate&ut
m_campaign=Lovebabbarcodestudio
#include <bits/stdc++.h>
solve(myStack,x,count+1,n);
myStack.push(num);
}
int count = 0;
solve(myStack,x,count,n);
return myStack;
______________________________________________________________________________Q.
43) Reverse Stack Using Recursion
https://www.codingninjas.com/codestudio/problems/reverse-stack-using-
recursion_631875?topList=love-babbar-dsa-sheet-problems&leftPanelTab=0%3Fsource
%3Dyoutube&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliate&ut
m_campaign=Lovebabbarcodestudio
insertAtBottom(stack,top);
stack.push(num);
}
//recursive call
reverseStack(stack);
insertAtBottom(stack,top);
}
___________________________________________________________________Q. 44 Sort a
Stack
https://www.codingninjas.com/codestudio/problems/sort-a-stack_985275?topList=love-
babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium
=affiliate&utm_campaign=Lovebabbarcodestudio
#include <bits/stdc++.h>
int t = stack.top();
stack.pop();
sortInsert(stack,element);
stack.push(t);
}
//recursion call
sortStack(stack);
sortInsert(stack,top);
_________________________________________________________________________________Q.
45) Redundant bracket
https://www.codingninjas.com/codestudio/problems/redundant-brackets_975473?
leftPanelTab=0%3Fsource
%3Dyoutube&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliate&ut
m_campaign=Lovebabbarcodestudio
#include <bits/stdc++.h>
bool findRedundantBrackets(string &s)
{
stack<char> st;
for(int i=0; i<s.size(); i++){
char ch = s[i];
if(ch == '(' || ch == '+' || ch == '-' || ch == '*' || ch == '/'){
st.push(ch);
}
else{
if(ch == ')'){
bool isRedundant = true;
while(st.top()!='('){
___________________________________________________________________________________
___Q.46 Minimum Cost To Make String Valid
https://www.codingninjas.com/codestudio/problems/minimum-cost-to-make-string-
valid_1115770?
leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium=affiliat
e&utm_campaign=Lovebabbarcodestudio
#include <bits/stdc++.h>
int findMinimumCost(string str) {
if(str.size()%2 == 1){
return -1;
}
stack<char> s;
for(int i=0; i<str.size(); i++){
char ch = str[i];
if(ch == '{'){
s.push(ch);
}
else{
if(!s.empty() && s.top() == '{'){
s.pop();
}
else{
s.push(ch);
}
}
}
//invalid string left
int a=0,b=0;
while(!s.empty()){
if(s.top()=='{'){
a++;
}
else{
b++;
}
s.pop();
}
int ans = (a+1)/2 + (b+1)/2;
return ans;
}
_______________________________________________________________________________Q.
47) Next Smaller Element
https://www.codingninjas.com/codestudio/problems/next-smaller-element_1112581?
topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio&utm_source=youtube&utm_medium
=affiliate&utm_campaign=Lovebabbarcodestudio
#include<bits/stdc++.h>
vector<int> nextSmallerElement(vector<int> &arr, int n)
{
stack<int> s;
s.push(-1);
vector<int> ans(n);
__________________________________________________________________________________Q
.48) Largest Rectangle in Histogram
https://leetcode.com/problems/largest-rectangle-in-histogram/description/
vector<int> next(n);
next = nextSmallElement(heights,n);
vector<int> prev(n);
prev = prevSmallElement(heights,n);
________________________________________________________________________________Q.4
9) The Celebrity Problem
https://practice.geeksforgeeks.org/problems/the-celebrity-problem/1
}
while(s.size()>1){
int a = s.top();
s.pop();
int b = s.top();
s.pop();
if(M[a][b] == 1){
s.push(b);
}
else{
s.push(a);
}
}
_______________________________________________________________________________Q.50
) Max rectangle
https://practice.geeksforgeeks.org/problems/max-rectangle/1
vector<int> next(n);
next = nextSmallElement(heights,n);
vector<int> prev(n);
prev = prevSmallElement(heights,n);
________________________________________________________________________________Q.5
1) N Stacks In An Array
https://www.codingninjas.com/codestudio/problems/n-stacks-in-an-array_1164271?
topList=love-babbar-dsa-sheet-
problems&leftPanelTab=0&campaign=Lovebabbarcodestudio11thfeb&utm_source=youtube&utm
_medium=affiliate&utm_campaign=Lovebabbarcodestudio11thfeb
#include <bits/stdc++.h>
class NStack
{
int *arr;
int *top;
int *next;
int freespot;
int n,s;
public:
// Initialize your data structure.
NStack(int N, int S)
{
n = N;
s = S;
arr = new int[s];
top = new int[n];
next = new int[s];
//top initialise
for(int i=0; i<n; i++){
top[i] = -1;
}
//next initialise
for(int i=0; i<s-1; i++){
next[i] = i+1;
}
next[s-1] = -1;
freespot = 0;
}
// Pushes 'X' into the Mth stack. Returns true if it gets pushed into the
stack, and false otherwise.
bool push(int x, int m)
{
if(freespot == -1){
return false;
}
//find index
int index = freespot;
//insert in array
arr[index] = x;
//update freespot
freespot = next[index];
//update next
next[index] = top[m-1];
//update top
top[m-1] = index;
return true;
}
// Pops top element from Mth Stack. Returns -1 if the stack is empty, otherwise
returns the popped element.
int pop(int m)
{
if(top[m-1] == -1){
return -1;
}
top[m-1] = next[index];
next[index] = freespot;
freespot = index;
return arr[index];
}
};