Week - 08-Learn Dsa With C++
Week - 08-Learn Dsa With C++
LEARN DSA
WITH C++
#STACK : A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
Create stack
Index = 0 1 2 3 4
top = -1;
Push = 7; top = 0;
Push = 13; top= 1; [increase]
Same way …… Insert elem at front..
class Stack
{
int top;
int *arr;
int size;
public:
Stack(int s)
{
arr = new int(s);
top = -1;
size = s;
};
// Push element in stack
void push(int data)
{
if (top == size - 1)
{
cout << "Stack Overflow\n";
return;
}
top++;
arr[top] = data;
}
return 0;
};
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
next = NULL;
}
};
class Stack
{
Node *top;
public:
Stack()
{
top = NULL;
}
// push element
void push(int data)
{
Node *temp = new Node(data);
if (!temp)
{
cout << "Stack Underflow\n";
return;
}
temp->next = top;
top = temp;
}
// pop element
void pop()
{
if (!top)
{
cout << "Satack Underflow\n";
return;
}
Node *temp = top;
top = top->next;
delete temp;
}
// top element
int peek()
{
if (!top)
{
cout << "Stack is Empty\n";
}
return top->data;
};
bool empty()
{
return top == NULL;
}
};
int main()
{
Stack s;
s.push(10);
s.push(30);
cout<<s.peek()<<endl;
cout << s.empty();
return 0;
};
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int>s;
s.push(55);
s.push(11);
s.push(33);
s.push(44);
s.pop();
cout<<s.top()<<endl;
cout<<s.empty();
return 0;
};
while(count--)
{
temp.push(s.top());
s.pop();
};
s.pop();
while(temp.size())
{
s.push(temp.top());
temp.pop();
}
}
};
Redundant Braces :: < Interviewbit >
int Solution::braces(string A) {
stack<char>s;
int count, i=0, n=A.size();
while(i<n)
{
if(A[i]!=')')
s.push(A[i]);
else
{
count =0;
while(s.size() && s.top() != '(')
count++, s.pop();
if(count<3)
return 1;
s.pop();
s.push('a');
}
i++;
}
return 0;
}
LEARN DSA WITH C++
WEEK :: 08 DAY: 02 DATE: 06-06-2023
STACK MEDIUM LEVEL QUESTION
class Solution{
int minEle;
stack<int> s;
public:
/*returns min element from stack*/
int getMin(){
while(K--)
st.pop();
string ans;
char c;
while(st.size())
{
c = '0' + st.top();
st.pop();
ans+= c;
};
i = ans.size() -1;
while(i>=0 && ans[i] == '0')
{
ans.pop_back();
i--;
};
reverse(ans.begin(), ans.end());
if(ans.size()==0)
return "0";
else
return ans;
}
};
Clumsy Factorial << LeetCode>>
class Solution {
public:
int clumsy(int n) {
stack<int>s;
int num, i=0;
s.push(n);
n--;
while(n)
{
if(i==0)
{
num = s.top();
s.pop();
s.push(num*n);
}
else if(i==1)
{
num = s.top();
s.pop();
s.push(num/n);
}
else
s.push(n);
i =(i+1)%4;
n--;
};
stack<int>ans;
while(s.size())
{
ans.push(s.top());
s.pop();
};
int sum = ans.top();
ans.pop();
bool flag =0;
while(ans.size())
{
if(flag==0)
sum+= ans.top();
else
sum-= ans.top();
ans.pop();
flag =!flag;
}
return sum;
}
};
Maximum Rectangular Area in a Histogram << GeeksforGeeks >>
class Solution {
public:
void push_right(long long arr[], int n, int right[]) {
stack<int> s;
int i = 0;
while (i < n) {
if (s.empty() || arr[i] >= arr[s.top()]) {
s.push(i);
i++;
} else {
while (!s.empty() && arr[i] < arr[s.top()]) {
right[s.top()] = i;
s.pop();
}
}
}
while (!s.empty()) {
right[s.top()] = n;
s.pop();
}
}
while (i >= 0) {
if (s.empty() || arr[i] >= arr[s.top()]) {
s.push(i);
i--;
} else {
while (!s.empty() && arr[i] < arr[s.top()]) {
left[s.top()] = i;
s.pop();
}
}
}
while (!s.empty()) {
left[s.top()] = -1;
s.pop();
}
}
push_right(arr, n, right);
push_left(arr, n, left);
long long area = 0;
delete[] right;
delete[] left;
return area;
}
};
while(i<n)
{
if(s.size()==0)
s.push(i);
else
{
if(arr[i]>= arr[s.top()])
s.push(i);
else
{
while(s.size() && arr[i]< arr[s.top()])
{
right[s.top()] = i;
s.pop();
};
s.push(i);
}
}
i++;
}
while(s.size())
{
right[s.top()] = n;
s.pop();
}
}
push_right(sum, m, right);
push_left(sum, m, left);
return area;
}
};
The Celebrity Problem << GeeksforGeeks >>
class Solution
{
public:
//Function to find if there is a celebrity in the party or not.
int celebrity(vector<vector<int> >& M, int n)
{
// code here
stack<int>s;
for(int i=0; i<n; i++)
s.push(i);
int first, second;
while(s.size()>1)
{
first = s.top();
s.pop();
second = s.top();
s.pop();
if(M[first][second] && !M[second][first])
s.push(second);
else if(M[second][first] && !M[first][second])
s.push(first);
};
if(s.size() ==0)
return -1;
The queue data structure follows the FIFO (First In First Out) principle where elements that are added first
will be removed first.
Front/rear
0 1 2 3 4 5
2 4
Front rear
0 1 2 3 4 5
2 4 8
Front rear
0 1 2 3 4 5
2 4 8 4 1 9
Front
rear
Void Push (int x)
{
if(rear == size)
{
cout<<”Queue is full”;
return;
arr[rear] = x;
rear++;
}
Void pop( )
{
if(rear == font)
{ front = rear =0;
cout<<”Queue is Empty”;
return ; }
}
Circular Queue ::
0 1 2 3 4
front/rear
0 1 2 3 4
4 8
Front rear
0 1 2 3 4
4 8 6 7
Front rear
0 1 2 3 4
4 8 6 7
Front rear
0 1 2 3 4
8 6 7
Front rear
0 1 2 3 4
1 8 6 7 4
rear Front front==rear -> Queue is full
So we initialize rear and front = -1;
0 1 2 3 4
Pop Operation ::
Void pop()
{
if(Empty() )
{
Cout <<”Queue is empty”;
}
Else if (front =rear)
{
Front =rear = -1;
}
Else
{
Front = (front + 1) % size;
return;
}
#Function::
bool Empty()
{
return front ==-1 && rear == -1;
}
bool Full()
{
return front == (rear +1) % size;
}
class CircularQueue
{
int *arr;
int front;
int rear;
int size;
public:
CircularQueue(int size)
{
front = rear = -1;
arr = new int[size];
this->size = size;
}
bool empty()
{
return front ==-1 && rear == -1;
}
bool full()
{
return front == (rear+1) % size;
}
void pop()
{
if(empty())
{
cout<<"Queue is Empty\n";
return;
}
else if(front==rear)
{
cout<<"Element is popped "<<arr[front]<<endl;
front = rear = -1;
return;
}
else
{
cout<<"Element is popped "<<arr[front]<<endl;
front = (front +1)%size;
return;
}
}
};
int main()
{
CircularQueue q(5);
q.push(10);
q.push(11);
q.push(12);
q.push(13);
q.push(14);
q.push(15);
q.pop();
q.push(17);
q.push(18);
return 0;
}
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int>q;
q.push(10);
q.push(11);
q.push(12);
q.push(13);
q.push(14);
q.push(15);
q.pop();
q.push(17);
q.push(18);
cout<<q.front()<<endl;
cout<<q.size()<<endl;
cout<<q.empty()<<endl;
return 0;
};
Queue Reversal :: <GeeksforGeeks>
class Solution
{
public:
queue<int> rev(queue<int> q)
{
// add code here.
stack<int>s;
while(q.size())
{
s.push(q.front());
q.pop();
}
while(s.size())
{
q.push(s.top());
s.pop();
};
return q;
}
};
if(s2.size())
{
int data = s2.top();
s2.pop();
return data;
};
while(s1.size())
{
s2.push(s1.top());
s1.pop();
};
int data = s2.top();
s2.pop();
return data;
}
LEARN DSA WITH C++
WEEK :: 08 DAY: 05 DATE: 12-06-2023
QUEUE ADVANCE
//Function to find starting point where the truck can start to get through
//the complete circle without exhausting its petrol in between.
int tour(petrolPump p[],int n)
{
//Your code here
int deficit =0;
int balance = 0;
int start = 0;
else
return -1;
}
};
DEQUE
4 7 6 1
5 4 7 6 1
5 4 7 6 1 6
Operation ::
d.push_back(6);
d.push_front(5);
d.pop_back(5);
d.pop_front();
d.front();
d.back();
#include<iostream>
#include<deque>
using namespace std;
int main()
{
deque<int>q;
q.push_back(5);
q.push_front(11);
cout<<q.front()<<endl;
cout<<q.back()<<endl;
cout<<q.size()<<endl;
q.pop_back();
q.pop_front();
cout<<q.size()<<endl;
return 0;
};
if(q.empty())
ans.push_back(0);
else
ans.push_back(A[q.front()]);
deque<int> window;
int n = A.size();
// Remove elements smaller than the current element from the back of the
deque
while (!window.empty() && A[i] >= A[window.back()]) {
window.pop_back();
}
window.push_back(i);
C.push_back(A[window.front()]);
}
return C;
}