Posts

Showing posts with the label easy

Intro to Hashing problems made easy

/*--- Below are 3 problems i have used hasing technique, After seeing below problems you'll be 100% clear on the basic hasing needed for competative programming First Repeating Element  Given an array arr[] of size N. The task is to find the first repeating element in an array of integers, i.e., an element that occurs more than once and whose index of first occurrence is smallest. Input : The first line contains an integer T denoting the total number of test cases. In each test cases, First line is number of elements in array N and second its values. Output: In each separate line print the index of first repeating element, if there is not any repeating element then print “-1” (without quotes). Use 1 Based Indexing. Constraints: 1 <= T <= 500 1 <= N <= 106 0 <= Ai <= 106 Example: Input: 1 7 1 5 3 4 3 5 6 Output: 2 Explanation: Testcase 1: 5 is appearing twice and its first appearence is at index 2 which is less than 3 whose first occur...

Never Two consecutive , Include Exclude technique

/* Stickler the thief wants to loot money from a society having n houses in a single line.  He is a weird person and follows a certain rule when looting the houses.  According to the rule, he will never loot two consecutive houses. At the same time,   he wants to maximize the amount he loots. The thief knows which house has what amount of money   but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money   he can get if he strictly follows the rule. Each house has a[i] amount of money present in it. */ #include<bits/stdc++.h> // this is clearly an consec include exclude problem using namespace std; int maxsumnotconsec(int a[], int n) {     int incl=a[0];     int exec=0;     for(int i=1;i<n;i++)     {         int inc_new=exec+a[i];         // we can incude the new element(latest ele) if and only if we have ex...

Stack using Queues

#include<bits/stdc++.h> using namespace std; class Q { stack<int> s1,s2; public :void enque(int a) { s1.push(a); } public:int deque() { int j=-1; while(s1.size()<=1) { int a= s1.top(); s1.pop(); s2.push(a); } if(s1.size()==1) { j=s1.top(); s1.pop(); } while(!s2.empty()) { int a= s1.top(); s1.pop(); s2.push(a); } return j; } }; int main() { Q q1; int r=0; while(r!=3) { cout<<"1. Enque"<<endl; cout<<"2.Deque"<<endl; cout<<"3. exit"<<endl; cin>>r; if(r==1) {int e; cout<<"Enter an element to insert in queue"; cin>>e; q1.enque(e); } else if(r==2) { cout<<q1.deque()<<endl; } } }

Get minimum of stack in Reasonable time

// what if a product based  company asks that they want the minimum of stack in resonalble time.. // if you knew this program congrats... you got job in product based company #include<iostream> #include<stack> using namespace std; void push(int a); bool isFull(int n); bool isEmpty(); int pop(); int getMin(); stack<int> s; int main(){ int t; cin>>t; while(t--){ int n,a; cin>>n; while(!isEmpty()){     pop(); } while(!isFull(n)){ cin>>a; push(a); } cout<<getMin()<<endl; } } /*This is a function problem.You only need to complete the function given below*/ /*Complete the function(s) below*/ void push(int a) {      s.push(a); } bool isFull(int n) {     return (s.size()==n); } bool isEmpty() {return s.empty();     } int pop() {     int a=s.top();     s.pop();     return a ;     } in...

Stack - print bracket number

/ * example 2 (0+1)*0(0+1)*0(0+1)* (1*0)*1* o/p 1 1 2 2 3 3  1 1  Example: Input: 3 (a+(b*c))+(d/e) ((())(())) ((((() Output: 1 2 2 1 3 3 1 2 3 3 2 4 5 5 4 1 1 2 3 4 5 5 */ #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     string s;     stack <int> st;     int c=0; // we maintain the count of open brackets     cin>>s;     for(int i=0;i<s.length();i++)     {         if(s[i]=='(')         {c++;         cout<<c<<" ";         st.push(c);         }         if(s[i]==')')         {             cout<<st.top()<<" ";             st.pop();         } ...