Posts

STL's EASY WAY TO FIND maximum & minimum IN AN ARRAY

* max_element ( a , a + n )-* min_element ( a , a + n )
ONE_ZERO SPOJ  http://www.spoj.com/problems/ONEZERO/ It's very important to make a visited array [1...n], so that the number which is visited once will not be visited again, if we would not make this visited it will definitely lead to TLE :P (idea credit kishlay ) solution #include<bits/stdc++.h> using namespace std; string bfs(int n) {  pair<int,string> temp;  queue<pair<int,string> > st;  st.push(make_pair(1,"1"));  int visited[n];  for(int i=0;i<n;i++) visited[i]=0; visited[1]=1;  while(!st.empty())  {   string tt1,tt2;   temp=st.front();   st.pop();   if(temp.first%n!=0)   {     tt1=temp.second+"0";     //cout<<tt<<" ";     if(visited[(temp.first*10)%n]==0){     st.push(make_pair((temp.first*10)%n,tt1));     visited[(temp.first*10)%n]=1;}     ...

UNION FIND FOR CODECHEF QUESTION http://www.codechef.com/ALMA2015/problems/ALMA03/

chota bheem and gcd ( https://www.codechef.com/problems/ALMA03  ) #include <bits/stdc++.h> using namespace std; #define MAXN 205 #define MOD 1000000007 int parent [ MAXN ] ,A [ MAXN ] ; void init ( ) { for ( int i= 0 ;i<MAXN;++i ) parent [ i ] = - 1 ; } int find ( int i ) { if ( parent [ i ] ==- 1 ) return i; else return parent [ i ] = find ( parent [ i ] ) ; } void merge ( int x, int y ) { x = find ( x ) ; y = find ( y ) ; if ( x!=y ) { if ( A [ x ] >A [ y ] ) parent [ y ] = x; /// whose rank is greater will become parent (here Rank work is done by Cost itself) else parent [ x ] = y; } } int main ( ) { int t,n; scanf ( "%d" ,&t ) ; while ( t-- ) { init ( ) ; scanf ( "%d" ,&n ) ; for ( int i= 0 ;i<n;++i )  { scanf ( "%d" ,A+i ) ; } for ( int i= 0 ;i<n;++i ) for ( in...
nCr  find  karne  ka  best  example. #include<bits/stdc++.h> using namespace std; int fast_pow(long long base, long long n,long long M) {     if(n==0)        return 1;     if(n==1)     return base;     long long halfn=fast_pow(base,n/2,M);     if(n%2==0)         return ( halfn * halfn ) % M;     else         return ( ( ( halfn * halfn ) % M ) * base ) % M; } int findMMI_fermat(int n,int M) {     return fast_pow(n,M-2,M); } int main() {     long long fact[100001];     fact[0]=1;     int i=1;     int MOD=1000000007;     while(i<=100000)     {         fact[i]=(fact[i-1]*i)%MOD;         i++;     }     int t;     cin>>t;     while(t--)   ...
( BFS  APP. 3rd)  COUNT  NUMBER  OF  CONNECTED  COMPONENTS .  SPOJ (PRAYATNA  PR)    USING  ADJ.  LIST #include<bits/stdc++.h> using namespace std; vector<int> vv[100000]; void bfs(int src,int v,int visited[],int lebel[]) {  int i,rem;  lebel[src]=0;  visited[src]=1;  queue<int> q;  q.push(src);  while(!q.empty())  {   rem=q.front();   q.pop();   for(i=0;i<vv[rem].size();i++)   {    if(visited[ vv[rem][i] ]!=1)    {      lebel[vv[rem][i]]=lebel[rem]+1;      q.push(vv[rem][i]);      visited[vv[rem][i]]=1;    }   }  } } int main() { int t,hh; cin>>t; for(hh=1;hh<=t;hh++) {  int i,j,p,q,rem,v,e,flag=0,gco=1;  cin>>v>>e;   int lebel[v];   int visited[v];  for(i=0;i<v;i++) ...
DIJKSTRA  IN 2 D  MATRIX  USING  PRIORITY QUEUE :) SPOJ SHOPPING #include<bits/stdc++.h> #define f first #define s second using namespace std; int indx[]={-1,0,0,+1}; int indy[]={0,+1,-1,0}; int w,h; string a[1000]; #define pp pair<int,pair<int,int> > class Prioritize { public:     int operator() ( const pair<int,pair<int,int> >& p1, const pair<int,pair<int,int> >& p2 )     {         return p1.first > p2.first;     } }; void dij(int x,int y,int x1,int y1) {   //cout<<"gaya"<<endl;  int visited[h][w];  int i,j,dt1,dt2,dt3;  for(i=0;i<h;i++)    for(j=0;j<w;j++)      visited[i][j]=0;  int dist[h][w];  for(i=0;i<h;i++)    for(j=0;j<w;j++)      dist[i][j]=INT_MAX;   visited[x][y]=1;   dist[x][y]=0;   priority_q...
STL  SET  QUESTION   SPOJ   FRIENDS OF  FRIEND #include<bits/stdc++.h> using namespace std; int main() { int n,i,k,m,j; cin>>n; set<int> s; for(i=0;i<n;i++) { cin>>k; s.insert(k);  cin>>m;  for(j=0;j<m;j++)  {   cin>>k;   s.insert(k);  } }  //for (std::set<int>::iterator it=s.begin(); it!=s.end(); ++it)    // std::cout << ' ' << *it;       cout<<s.size()-n<<endl; return 0; }