Skip to main content

Posts

Showing posts from 2019

codeforces div3 round 547-Superhero Battle

#include < bits / stdc ++. h > using namespace std ; int main () { long long H ; int n , i ; cin >> H >> n ; long long d [ n ], sum = 0 , h = H , gap = 0 ; for ( i = 0 ; i < n ; i ++) { cin >> d [ i ]; sum -= d [ i ]; h = h + d [ i ]; if ( h <= 0 ) { cout << i + 1 << endl ; return 0 ; } gap = max ( gap , sum ); } if ( sum <= 0 ) { cout << "-1" << endl ; return 0 ; } long long whole =( H - gap )/ sum ; H = H - whole * sum ; long long result = whole * n ; for ( i = 0 ;; i ++) { H = H + d [ i % n ]; result ++; if ( H <= 0 ) { cout << result << endl ; break ; } } return 0 ; }

codeforces-beautiful year

#include < bits / stdc ++. h > using namespace std ; bool check ( int n ) { int count [ 10 ]={ 0 }; while ( n != 0 ) { int t = n % 10 ; count [ t ]++; n = n / 10 ; } int flag = 1 ; for ( int i = 0 ; i < 10 ; i ++) { if ( count [ i ]> 1 ) { return false ; flag = 0 ; break ; } } if ( flag == 1 ) return true ; } int main () { int y ; cin >> y ; int i ; if ( check ( y )) y ++; for ( i = y ;; i ++) { if ( check ( i )) { cout << i ; break ; } } return 0 ; }

codeforces-expression

#include < bits / stdc ++. h > using namespace std ; void check ( int a , int b , int c ) { int p , q , r , s , t , u ; p = a * b + c ; q = a + b * c ; r =( a + b )* c ; s = a * b * c ; t = a *( b + c ); u = a + b + c ; cout << max ( p , max ( q , max ( r , max ( s , max ( t , u ))))); } int main () { int a , b , c ; cin >> a >> b >> c ; check ( a , b , c ); return 0 ; }

codeforces-presents

#include < bits / stdc ++. h > using namespace std ; int main () { int n , i ; cin >> n ; vector < pair < int , int >> vect ; for ( i = 0 ; i < n ; i ++) { int x ; cin >> x ; vect . push_back ( make_pair ( x , i + 1 )); } sort ( vect . begin (), vect . end ()); for ( i = 0 ; i < n ; i ++) { cout << vect [ i ]. second << " " ; } return 0 ; }

codeforces-puzzles solution

#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std ; int main () { int n , m ; cin >> n >> m ; vector <int> vect ; map < int , int > mp ; for ( int i = 0 ; i < m ; i ++) { int x ; cin >> x ; mp [ x ]++; vect . push_back ( x ); } sort ( vect . begin (), vect . end ()); int small = vect [ n - 1 ]- vect [ 0 ]; for ( int i = 0 ; i < vect . size ()- n + 1 ; i ++) { int d = vect [ i + n - 1 ]- vect [ i ]; if ( small > d ) small = d ; } int flag = 1 ; for ( auto k : mp ) { if ( k . second >= n ) { cout << "0" ; flag = 0 ; break ; } } if ( flag == 1 ) { cout << small ; } return 0 ; }

codeforces-Word

#include <iostream> using namespace std ; void process1 ( char str []) { int i = 0 ; while ( str [ i ]!= '\0' ) { if ( str [ i ]>= 65 and str [ i ]<= 90 ) { str [ i ]= str [ i ]+ 32 ; } i ++; } } void process2 ( char str []) { int i = 0 ; while ( str [ i ]!= '\0' ) { if ( str [ i ]>= 97 and str [ i ]<= 122 ) { str [ i ]= str [ i ]- 32 ; } i ++; } } void check ( char str []) { int i = 0 , count = 0 , count1 = 0 ; while ( str [ i ]!= '\0' ) { if ( str [ i ]>= 65 and str [ i ]<= 90 ) { count ++; } else if ( str [ i ]>= 97 and str [ i ]<= 122 ) { count1 ++; } i ++; } if ( count1 == count ) process1 ( str ); else if ( count1 < count ) process2 ( str ); else if ( count1 > count ) process1 ( str ); } int main () { char str [ 101 ]; cin >> str ; check ( str ); cout << str ;

codeforces-infinite sequence

#include <iostream> using namespace std ; int main () { long long a , b , c ; cin >> a >> b >> c ; if ( c == 0 ) { if ( a == b ) cout << "YES" ; else cout << "NO" ; } else { if (( b - a )% c == 0 and ( b - a )/ c >= 0 ) { cout << "YES" ; } else { cout << "NO" ; } } return 0 ; }

codeforces-calculating function

#include <iostream> #include <cmath> using namespace std ; long long fun ( long long n ) { if ( n % 2 == 0 ) return n / 2 ; else return - n + fun ( n - 1 ); } int main () { long long n , sum = 0 ; cin >> n ; cout << fun ( n ); return 0 ; }

codeforces-insomnia cure

#include <iostream> using namespace std ; int main () { int k , l , m , n ; long d , count = 0 ; cin >> k >> l >> m >> n >> d ; for ( long i = 1 ; i <= d ; i ++) { if ( i % k != 0 and i % l != 0 and i % m != 0 and i % n != 0 ) count ++; } cout << d - count ; return 0 ; }

codeforces-even odds

#include <iostream> using namespace std ; int main () { long long n , k ; cin >> n >> k ; long long t =( n + 1 )/ 2 ; if ( k <= t ) cout << 2 * k - 1 ; else cout <<( k - t )* 2 ; return 0 ; }

Codeforces-Dubstep solution

#include < bits / stdc ++. h > using namespace std ; int main () { string str ; getline ( cin , str ); int index = str . find ( "WUB" ); int count = 0 ; while ( index !=- 1 ) { str . replace ( index , 3 , " " ); //str.erase(str.begin()+index,str.begin()+index+3); index = str . find ( "WUB" ); } cout << str ; return 0 ; }

codeforces-Anton and Danik

#include < bits / stdc ++. h > using namespace std ; int main () { long n ; string str ; cin >> n ; cin >> str ; long count = 0 , index ; index = str . find ( "A" ); while ( index !=- 1 ) { count ++; str . erase ( str . begin ()+ index , str . begin ()+ index + 1 ); index = str . find ( "A" ); } if ( count > n - count ) cout << "Anton" ; else if ( count < n - count ) cout << "Danik" ; else cout << "Friendship" ; return 0 ; }

codeforces-divide it solution

#include < bits / stdc ++. h > using namespace std ; void fun ( long long n , int count ) { if ( n == 1 ) cout << count << endl ; else if ( n % 2 != 0 and n % 3 != 0 and n % 5 != 0 ) cout << "-1" << endl ; if ( n % 5 == 0 ) { n =( 4 * n )/ 5 ; count ++; fun ( n , count ); } else if ( n % 3 == 0 ) { n =( 2 * n )/ 3 ; count ++; fun ( n , count ); } else if ( n % 2 == 0 ) { n = n / 2 ; count ++; fun ( n , count ); } } int main () { int q ; cin >> q ; for ( int i = 1 ; i <= q ; i ++) { long long x ; cin >> x ; fun ( x , 0 ); } return 0 ; }

codeforces-merge it solution

#include < bits / stdc ++. h > using namespace std ; int main () { int t ; cin >> t ; for ( int i = 1 ; i <= t ; i ++) { int count [ 3 ]={ 0 }; int n ; cin >> n ; for ( int j = 0 ; j < n ; j ++) { long x ; cin >> x ; count [ x % 3 ]++; } int result = count [ 0 ]; int t = min ( count [ 1 ], count [ 2 ]); result += t ; count [ 1 ]-= t ; count [ 2 ]-= t ; result +=( count [ 1 ]+ count [ 2 ])/ 3 ; cout << result << endl ; } return 0 ; }

Codeforces round 565 div 3-lose it

#include<iostream> #include<map> using namespace std; int main() {     int arr[6]={0};     map<int,int> m;     m[4]=0;     m[8]=1;     m[15]=2;     m[16]=3;     m[23]=4;     m[42]=5;     int n;     cin>>n;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];         if(a[i]==4)             arr[0]++;         else if(arr[m[a[i]]-1]>0)         {             arr[m[a[i]]-1]--;             arr[m[a[i]]]++;         }     }     cout<<n-6*arr[5];     return 0; }

Counting the number of strongly connected sub graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);         adj[w].push_back(v);     }     void dfs(int start,bool visited[]);     void counted(int start); }; void graph:: dfs(int start,bool visited[]) {     visited[start]=true;     for(auto it=adj[start].begin();it!=adj[start].end();it++)     {         if(!visited[*it])         {             dfs(*it,visited);         }     } } void graph:: counted(int start) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     int count=0;     visited[start]=true;     for(int k=0;k<v;k++)     {         if(!visited[k])         {             dfs(k,visited);             count++;         }     }     cout<<"count is "<<count<<endl; } int main() {     int v;     cout<<"E

DFS using a stack

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void dfs(int start); }; void graph:: dfs(int start) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     visited[start]=true;     stack<int> s;     s.push(start);     while(!s.empty())     {         int k=s.top();         s.pop();         cout<<k<<" ";         for(auto itr=adj[k].begin();itr!=adj[k].end();itr++)         {             if(!visited[*itr])             {                 visited[*itr]=true;                 s.push(*itr);             }         }     } } int main() {     int v;     cout<<"Enter the number of vertices"<<endl;     cin>>v;     graph g(v);     while(1)     {         int x,y;         cout<<"Ent

DFS of a graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void dfs(int v);     void dfsSearch(int v,bool visited[]); }; void graph:: dfsSearch(int start,bool visited[]) {     visited[start]=true;     cout<<start<<" ";     for(auto itr=adj[start].begin();itr!=adj[start].end();itr++)     {         if (!visited[*itr])         {             dfsSearch(*itr,visited);         }     } } void graph:: dfs(int start) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     dfsSearch(start,visited); } int main() {     int v;     cout<<"Enter the number of vertices"<<endl;     cin>>v;     graph g(v);     while(1)     {         int x,y;         cout<<"Enter the nodes"<<endl;        

BFS of a graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void bfs(int start); }; void graph:: bfs(int start) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     visited[start]=true;     list<int> queue;     queue.push_back(start);     while(!queue.empty())     {         int s=queue.front();         cout<<s<<" ";         queue.pop_front();         for(auto it=adj[s].begin();it!=adj[s].end();it++)         {             if(visited[*it]==false)             {                 visited[*it]=true;                 queue.push_back(*it);             }         }     } } int main() {     int v;     cout<<"Enter the number of vertices"<<endl;     cin>>v;     graph g(v);     while(1)     {      

Weighted graph using stl

#include<bits/stdc++.h> using namespace std; void add_edge(vector<pair<int,int>> adj[],int u,int v,int wt) {     adj[u].push_back(make_pair(v,wt));     adj[v].push_back(make_pair(u,wt)); } void print_graph(vector<pair<int,int>> adj[],int V) {     int v,w;     for(int i=0;i<V;i++)     {         cout<<"Node "<<i<<" makes an edge with"<<endl;         for(auto it=adj[i].begin();it!=adj[i].end();it++)         {             v=it->first;             w=it->second;             cout<<"node "<<v<<"with weight "<<w<<endl;         }         cout<<endl;     } } int main() {     int V;     cin>>V;     vector<pair<int,int>> adj[V];     while(1)     {         int x,y,w;         cout<<"Enter the nodes and weight"<<endl;         cin>>x>>y>>w;         add_edge(adj,x,y,w);         cout<<"Enter -1 to e

Adjacency matrix list

#include<bits/stdc++.h> using namespace std; void add_edge(vector<int> adj[],int u,int v) {     adj[u].push_back(v);     adj[v].push_back(u); } void print_graph(vector<int>adj[],int V) {     for(int i=0;i<V;i++)     {         for(auto x:adj[i])             cout<<x<<"->";         cout<<endl;     } } int main() {     int V;     cout<<"Enter the number of vertices"<<endl;     cin>>V;     vector<int> adj[V];     while(1)     {         int x,y;         cout<<"Enter the nodes"<<endl;         cin>>x>>y;         add_edge(adj,x,y);         cout<<"Enter -1 to exit"<<endl;         int t;         cin>>t;         if(t==-1)             break;     }     print_graph(adj,V);     return 0; }

Finding the mother node of a graph

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void dfs(int start,bool visited[]);     int find_mother(); }; void graph::dfs(int start,bool visited[]) {     visited[start]=true;     for(auto itr=adj[start].begin();itr!=adj[start].end();itr++)     {         if(!visited[*itr])         {             dfs(*itr,visited);         }     } } int graph::find_mother() {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     int m=0;     for(int i=0;i<v;i++)     {         if(visited[i]==false)         {             dfs(i,visited);             m=i;         }     }     for(int i=0;i<v;i++)         visited[i]=false;     dfs(m,visited);     for(int i=0;i<v;i++)     {         if(visited[i]==false)             return -1;     }     return

counting the number of nodes of a particular level

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);         adj[w].push_back(v);     }     int bfs(int root,int l); }; int graph:: bfs(int root,int l) {     bool *visited=new bool[v];     int level[v];     for(int i=0;i<v;i++)     {         visited[i]=false;         level[i]=0;     }     visited[root]=true;     list<int> queue;     queue.push_back(root);     int count=0;     while(!queue.empty())     {         int s=queue.front();         queue.pop_front();         for(auto it=adj[s].begin();it!=adj[s].end();it++)         {             if(!visited[*it])             {                 level[*it]=level[s]+1;                 visited[*it]=true;                 queue.push_back(*it);             }         }     }     for(int i=0;i<v;i++)     {         if(level[i]==l)      

Counting the paths between two nodes

#include<bits/stdc++.h> using namespace std; class graph {     int v;     list<int> *adj; public:     graph(int v)     {         this->v=v;         adj=new list<int>[v];     }     void add_edge(int v,int w)     {         adj[v].push_back(w);     }     void count(int start,int end,bool visited[],int& path);     int  cou(int start,int end); }; int graph:: cou(int start,int end) {     bool *visited=new bool[v];     for(int i=0;i<v;i++)         visited[i]=false;     int path=0;     count(start,end,visited,path);     return path; } void graph::count(int start,int end,bool visited[],int& path) {     visited[start]=true;     if(start==end)         path++;     else     {         for(auto it=adj[start].begin();it!=adj[start].end();it++)         {             if(!visited[*it])             {                 count(*it,end,visited,path);             }         }     }     visited[start]=false; } int main() {     graph g(4);     g.add_edge(0,1);     g.add_edge(0,2);  

Jee Main Experience

April 8th, the day for which I worked hard for 3 years finally came. I woke up early and revised chemistry for 2 hrs and solved 2–3 math problems. That's all I have done in the morning. I reached the exam centre. It was already filled with many students and anxious parents. Some students were calm. I found one guy still solving his coaching material. I was shocked. I tried to calm myself. I went into the hall. I sat in my place. I spent an hour and half sitting idle. Now the real story begins. The omr and the paper were supposed to be given at 9:15. We didn't get. 9:20 still we didn't get. 9:25 we got. Ok I started filling the details. All those formalities were done by 9:50. I'm not joking. This was the exact time. By then I thought I'm done for nothing. I started math. I solved the first question. I got it. It gave me some confidence. I started doing the other questions. They were lengthy. I started feeling uncomfortable. I randomly switched to ph

Gcd using recursion

#include<iostream> #include<algorithm> using namespace std; int gcd(int m,int n) {     if(max(m,n)%min(m,n)==0)     {         return min(m,n);     }     else     {         gcd(max(m,n)%min(m,n),min(m,n));     } } int main() {     int m,n;     cin>>m>>n;     cout<<gcd(m,n);     return 0; }

Euclid algorithm to find gcd

#include<iostream> #include<algorithm> using namespace std; int gcd(int m,int n) {     while(n%m!=0)     {         int r=n%m;         n=m;         m=r;     }     return m; } int main() {     int m,n;     cout<<"Enter two numbers"<<endl;     cin>>m>>n;     cout<<gcd(m,n);     return 0; }

Sorting stack using recursion

#include<iostream> #include<stack> using namespace std; void SortInsert(stack<int> &s,int ele) {     if(s.empty() or ele>s.top())     {         s.push(ele);     }     else     {         int temp=s.top();         s.pop();         SortInsert(s,ele);         s.push(temp);     } } void SortStack(stack<int> &s) {     if(!s.empty())     {         int temp=s.top();         s.pop();         SortStack(s);         SortInsert(s,temp);     } } void display(stack<int> s) {     while(!s.empty())     {         cout<<s.top()<<" ";         s.pop();     }     cout<<endl; } int main() {     stack<int> s;     int n;     cout<<"Enter the number of elements of stack"<<endl;     cin>>n;     for(int i=0;i<n;i++)     {         int x;         cin>>x;         s.push(x);     }     display(s);     SortStack(s);     display(s);     return 0; }

basic stack stl c++

//basic stack stl c++ #include<iostream> #include<stack> using namespace std; void display(stack<int> s) {     while(!s.empty())     {         cout<<s.top()<<" ";         s.pop();     }     cout<<endl; } int main() {     stack<int> s;     s.push(1);     s.push(4);     s.push(5);     display(s);        //display the stack elements     s.pop();      //removes the top element     display(s);     if(s.empty())         cout<<"The stack is empty"<<endl;     else         cout<<"stack is not empty"<<endl;     return 0; }

Reversing a stack using recursion

#include<bits/stdc++.h> using namespace std; stack<int> s2; void rev(stack<int> s) {     if(!s.empty())     {         int temp=s.top();         s.pop();         s2.push(temp);         rev(s);     } } void display(stack<int> s) {     while(!s.empty())     {         cout<<s.top()<<" ";         s.pop();     }     cout<<endl; } int main() {      stack<int> s;      int n;      cout<<"Enter the number of elements to push onto stack"<<endl;      cin>>n;      for (int i = 0; i < n; ++i)      {          /* code */          int x;          cin>>x;          s.push(x);      }      display(s);      rev(s);      display(s2);      return 0; }

Finding permutations of a string using recursion

#include<bits/stdc++.h> using namespace std; void print_perm(char str[],int k,int n) {     if(k==n)         cout<<str<<endl;     else     {         for(int i=k;i<n;i++)         {             swap(str[k],str[i]);             print_perm(str,k+1,n);             swap(str[k],str[i]);         }     } } int main() {     char str[20];     cout<<"Enter a string"<<endl;     cin>>str;     print_perm(str,0,strlen(str));     return 0; }

Bubble Sort Algorithm

//bubble sort algorithm in c++ #include<bits/stdc++.h> using namespace std; void bubble_sort(int arr[],int n) {     int i,j;     for(i=1;i<n;i++)     {         for(j=0;j<n-i;j++)         {             if(arr[j]>arr[j+1])             {                 swap(arr[j],arr[j+1]); //swap is an inbuilt function             }         }     }     //time complexity is O(n^2) } int main() {     int n,i;     cout<<"Enter the number of elements of the array"<<endl;     cin>>n;     int arr[n];     for(i=0;i<n;i++)     {         cin>>arr[i];     }     bubble_sort(arr,n);     cout<<"Now printting the elements of the sorted array"<<endl;     for(int p:arr)     {         cout<<p<<" ";     }     return 0; }