Sunday 1 December 2013

SubSet Sum Problem Including Negative Integers

Given a set of integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.

Solution:-
#include<iostream>
#include<vector>
using namespace std;

int compute(vector<long long>item , int n ,long long sum , long long osum)
{
    if(n < 0 )
    return sum==osum;
    else
    {
        if(sum == osum)
        return 1;
        
    }
    return compute(item , n-1 , sum + item[n] ,osum) || compute(item , n-1 , sum , osum);
    
}
    

int main()
{
    int T = 0 ;
    cin>>T;
    vector<long long>item;
    int ans = 0 ;
    int N = 0 ;
    long long osum;
    for(int t = 0 ; t < T ; t++)
    {
            cin>>N;
            item.clear();
            item.resize(N);
            for(long long i = 0 ; i < N ; i++)
            {
                    cin>>item[i];
            }
            cin>>osum;
            ans = compute(item ,N - 1, 0 , osum );
            if(ans == 1)
            cout<<"YES"<<endl;
            else
            cout<<"NO"<<endl;
    }
    
    return 0;

}


Wednesday 20 November 2013

SubSet Sum Problem Using Dynamic Programming

Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.

Solution:

#include<iostream>
#include<stdio.h>
using namespace std;

bool isSubsetSum(int set[], int n, int sum)
{
   
    bool subset[sum+1][n+1];
 
    // If sum is 0, then answer is true
    for (int i = 0; i <= n; i++)
      subset[0][i] = true;
 
    // If sum is not 0 and set is empty, then answer is false
    for (int i = 1; i <= sum; i++)
      subset[i][0] = false;
 
     
     for (int i = 1; i <= sum; i++)
     {
       for (int j = 1; j <= n; j++)
       {
         subset[i][j] = subset[i][j-1];
         if (i >= set[j-1])
           subset[i][j] = subset[i][j] || subset[i - set[j-1]][j-1];
       }
     }
 
    
 
     return subset[sum][n];
}

 
int main()
{
    int T = 0 ;
    cin>>T;
    int *a;
    int n = 0;
    int sum = 0 ;
    for(int t = 0 ; t < T ; t++)
    {
            cin>>n;
            a = new int[n];
            for(int i = 0 ; i < n ; i++)
            {
                    cin>>a[i];
            }
            
            cin>>sum;
  if (isSubsetSum(a , n , sum ) == true)
     cout<<"YES"<<endl;
  else
     cout<<"NO"<<endl;
     delete []a;
  }
  return 0;
}


Tuesday 19 November 2013

ZigZag Sequence Problem Top Coder 03 Semi Finals 3


Level Of the Problem ;-   EASY

A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.
For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.


SOLUTION:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int T = 0 ; 
    int N = 0 ;
    cin>>T;
    vector<int>num;
    vector<int> sol;
    vector<int>sign; // USED TO STORE THE SIGN OF LAST TWO ELEMENTS
    for(int t = 0 ; t < T ; t++)
    {
            cin>>N;
            num.clear();
            num.resize(N);
            sol.clear();
            sol.resize(N+1);
            sign.clear();
            sign.resize(N);
            for(int i = 0 ; i < N ; i++)
            {
                    cin>>num[i];
                    sol[i] = 1 ;
                    sign[i] = 0 ;
                    
            }
            sol[N]= 1 ;
            for(int i = 1 ; i < N ; i++)
            {
                    for(int j = 0 ; j < i ; j++)
                    {
                            if( j == 0)
                            {
                                
                                sign[i] = num[i] - num[j]; 
                                sol[i] = sol[j] + 1 ;
                            }
                            else if(  ( (sign[j] > 0) && (num[i] - num[j] < 0))  || ( (sign[j] < 0) && (num[i] - num[j] > 0) ) && ( sol[j] + 1 > sol[i]) )
                            {
                            sol[i] =  sol[j] +  1 ;
                            sign[i] = num[i] - num[j]; 
                                
                            }
                    }
            }  
            cout<<sol[N-1]<<endl;
    }
    cin.get();
    return 0;
}
                            

Thursday 11 July 2013

Program to implement Dijkstra's Algorithm in c++

#include<iostream>
#define SIZE 12
#define INFINITY 999
using namespace std;

class Queu
{
      public:
             Queu();
             int isempty();
             void insert(int node);
             int remove();
             void display();
      private:
              int front;
              int rear;
              int* queue;
};


Queu :: Queu()
{
     queue = new int[SIZE];
     front = 0 ;
     rear = 0 ;
   
}
void Queu :: insert(int node)
{
     if( (front == 1) && (rear == 11) )
     {
              cout << "Overflow" <<endl;
     }
     else if(front == 0)
     {
          queue[++front] = node;
          rear++;
     }
     else
     {
         queue[++rear] = node;
     }
           
}
int Queu :: remove()
{
     int item = 0;
     if(front == 0 )
     {
              cout << "underflow" <<endl;
     }
     else if(front == rear )
     {
          item = queue[rear];
          front = 0 ;
          rear = 0;
          return item;
     }
     else
     {
         item = queue[front++];
         return item;
     }
     return 0;
       
   
}
int Queu :: isempty()
{
    if(front == 0)
    {
             return 0;
    }
    else
    {
        return 1;
    }
}

void Queu :: display()
{
     cout<<"Queue is ";
     for(int i = front ; i <= rear ;  i++)
     {
             cout<<queue[front]<<" ";
     }
}

class Djalgo
{
      public:
             Djalgo();
             void insert();
             void search();
             void initialize();
      private:
              int vertices;
              int* dist;
              int** cost;
              int edges;
              int* visited;
              int *pvisit;
              int min();
              Queu Q;
};

Djalgo :: Djalgo()
{
       vertices = 0 ;
       dist = new int[SIZE];
       cost = new int*[SIZE];
       edges = 0 ;
       visited = new int[SIZE];
       pvisit = new int[SIZE];
}

void Djalgo :: initialize()
{
   
     int n = 0 ;
     int i = 0 , j = 0 ;
     cout << "Enter number of vertices " << endl;
     cin >> vertices ;
     cout << " Enter The vertices " << endl;
     for(int l = 0 ; l < SIZE  ; l ++)
     {
             cost[l] = new int [SIZE];
             visited[l] = 0;
             dist[l] = INFINITY;
             pvisit[l] = 0;
             if(l < vertices)
             {
             cin >> n ;
             Q.insert(n);
             }
     for(int p = 0 ; p < SIZE ; p++)
     {
             cost[l][p] = 0;
     }
           
     }
     cout << " Enter the number of edges" <<endl;
     cin>>edges;
     for(int k = 0 ; k < edges ; k++)
     {
             cout <<"Enter the edge" << endl;
             cin>>i>>j;
             cout<<"Its cost "<< endl;
             cin >> cost[i][j];
           
     }
}
int Djalgo :: min()
{
    int temp = INFINITY;
    int node = 1 ;
    int flag = 0;
    for( int i = 1 ; i < SIZE ; i ++)
    {
         if( (visited[i] == 0 ) )
         {
             if(temp > dist[i])
             {
                     temp = dist[i];
                     node = i;
             }
         }
    }
    return node;
}
           
void Djalgo :: search()
{
     int i = 1 , v = 0 , s = 0 ;
     cout << "Enter the source Vertex " <<endl;
     cin>>s;
     dist[s] = 0;
     while(Q.isempty())
     {
                       v = min();
                       cout<<"Current "<<v<<endl;
                 Q.remove();
                visited[v] = 1;
                 pvisit[i++] = v;
                 for(int k = 1 ; k < SIZE ; k++)
                 {
                         if( (cost[v][k] != 0) && (visited[k] == 0) )
                         {
                             if(dist[k] > dist[v] + cost[v][k])
                             {
                             dist[k] = dist[v] + cost[v][k];
                             }
                         }
                 }
     }
     for(int f = 1 ; f < i ; f++)
     {
             cout << "source vertex" << s << "Destination" << pvisit[f] << " Cost" << dist[pvisit[f]] << endl;
     }
   
}
                   
   
   

int main()
{
    Djalgo obj;
    obj.initialize();
    obj.search();
    cin.get();
    cin.get();
    return 0;
}

                                                                         BY: SAURABH BHATIA

Wednesday 10 July 2013

Program to implement Breadth First Search in C++

#include<iostream>
#define SIZE 12
using namespace std;

class Queu
{
      public:
             Queu();
             int isempty();
             void insert(int node);
             int remove();
             void display();
      private:
              int front;
              int rear;
              int* queue;
};

Queu :: Queu()
{
     queue = new int[SIZE];
     front = 0 ; 
     rear = 0 ; 
     
}
void Queu :: insert(int node)
{
     if( (front == 1) && (rear == 11) )
     {
              cout << "Overflow" <<endl;
     }
     else if(front == 0)
     {
          queue[++front] = node;
          rear++;
     }
     else
     {
         queue[++rear] = node;
     }
              
}
int Queu :: remove()
{
     int item = 0;
     if(front == 0 )
     {
              cout << "underflow" <<endl;
     }
     else if(front == rear )
     {
          item = queue[rear];
          front = 0 ; 
          rear = 0;
          return item;
     }
     else
     {
         item = queue[front++];
         return item;
     }
          
     
}
int Queu :: isempty()
{
    if(front == 0)
    {
             return 0;
    }
    else
    {
        return 1;
    }
}

void Queu :: display()
{
     cout<<"Queue is ";
     for(int i = front ; i <= rear ;  i++)
     {
             cout<<queue[front]<<" ";
     }
}
class Bfs
{
      public:
             Bfs();
             void insert();
             void search();
             void initialize();
             private:
                     int vertices;
                     int* status;
                     int** cost;
                     int edges;
                     int* pvisit; // used to store the parent of current processed node
};

      

Bfs :: Bfs ()
{
    vertices =  0 ; 
    edges = 0 ;  
    status = new int[SIZE];
    pvisit = new int [SIZE];
    cost = new int*[SIZE];
    initialize();
}

void Bfs :: initialize()
{
     for(int i = 0 ; i < SIZE ; i ++)
     {
             cost[i] = new int [SIZE];
             status[i] = 0 ; 
             pvisit[i] = 0 ;
     }

}
 

void Bfs :: insert()
{
     cout << "Enter Number of Vertices" << endl;
     cin >> vertices;
     cout << "Enter number of edges" << endl;
     cin >> edges;
     cout << "Enter the Edge " <<endl;
     int i = 0 , j = 0 ;
     for(int k = 0 ; k < edges ; k++)
     {
             cin>>i>>j;
             cost[i][j] = 1;
             
     
      }
}

void Bfs :: search()
{
     Queu Q;
     int v1 = 0 , v2 = 0 ;
     cout << "Enter the initial vertex" << endl;
     cin >> v1;
     cout<< "Enter the Final vertex " << endl;
     cin >> v2;
     Q.insert(v1);
     pvisit[v1] = v1;
     status[v1] = 1 ;
     int flag = 0;
     while(Q.isempty())
     {
                  v1 = Q.remove();
                  status[v1] = 2;
                  if(flag == 0 )
                  for(int i = 1 ; i <= vertices; i ++)
                  {
                          if( (cost[v1][i] == 1 ) && (status[i] == 0) )
                          {
                              Q.insert(i);
                              pvisit[i] = v1;
                              status[i] = 1 ;
                              if( i == v2)
                              {
                              flag = 1;
                              break;
                              }
                          }
                  }
                  else
                  break;
                  
     }
     cout << v2 << " " ;
     while(v2 != pvisit[v2])
     {
             cout << pvisit[v2] <<" " ;
             v2 = pvisit[v2];
     }

     
}

int main()
{
    Bfs obj;
    obj.initialize();
    obj.insert();
    obj.search();
    cin.get();
    cin.get();
    return 0;
}
                                                                       BY:  SAURABH BHATIA

Program to implement Depth First Search in C++

#include<iostream>
#define SIZE
using namespace std;
class Dfs
{
      public:
             Dfs();
             void insert();
             void traverse();
             void initialize();
             private:
                     int status*;
                     int cost**;
                     int stack*;
                     int top ;
                     int vertices;
                     int edges;
                     int k ;
                   
};

Dfs :: Dfs()
{
    top = 0 ;
    k = 0 ;
    edges = 0;
    vertices = 0 ;
    status = new int[SIZE];
    stack = new int[SIZE];
    cost  = new int*[SIZE];
    initialize();
 
}
void Dfs :: initialize()
{
     for(int i = 0 ; i < SIZE ; i++)
     {
             cost[i] = new int[SIZE];
             status[i] = 0 ;
     }
}
           
void Dfs :: insert()
{
   
     int i = 0 , j = 0 ;
     cout << "Enter number of vertices (1 - 12)" <<endl;
     cin>>vertices;
     cout << "Enter number of edges" << endl;
     cin>>edges;
     cout << "Enter the edges" << endl;
     for(k = 1 ; k <= edges ; k ++)
     {
     cin >> i >> j;
     cost[i][j]  = 1 ;
     }
}

void Dfs :: traverse()
{
     int v = 0 ;
     cout << "Enter The initial vertex" << endl;
     cin >> v;
     k = 0;

     stack[++top] = v;
     while(top != 0 )
     {
                status[v] = 2;
                cout << v <<" " ;
               for(int i  = 1 ; i <= 11 ; i++ )
               {
                     if( (cost[v][i] == 1) && (status[i] == 0) )
                     {
                       
                         stack[++top] = i ;
                         status[i] = 1 ;
                     }
               }
           
              v = stack[top--];
           
             
           
             
     }
}
int main()
{
    Dfs obj;
    obj.insert();
    obj.traverse();
    cin.get();
    cin.get();
    return 0 ;
}
                                                                        BY : SAURABH BHATIA

Tuesday 9 July 2013

Program to implement the traversals (Postorder , Inorder , Postorder) and searching in Binary Search Tree in c++

#include<iostream>
using namespace std;
class Btree
{
      private:
      int data;
      Btree *left;
      Btree *right;
      Btree *root;
      void insert(Btree* troot,int value);
      void destroy(Btree* troot);
      void preOrder(Btree *troot);
      void inOrder(Btree *troot);
      void postOrder(Btree *troot);
      void search(Btree *troot , int value);
     
      public:
             Btree();
             void insert(int value);
             void preOrder();
             void inOrder();
             void postOrder();
             void search(int value);
             void destroy();
           
};
     
     
      Btree::Btree()
      {
             data = 0;
       
             left = NULL;
             right = NULL;
             root = NULL;
      }
      void Btree:: insert(int value)
      {
           if( root == NULL )
           {
               Btree *temp = new Btree();
           temp -> data = value;
                  root = temp;
                  temp -> left = NULL;
                  temp -> right = NULL;
             
           }
           else
           {
               insert(root , value);
           }
       }
     
      void Btree :: insert( Btree *troot , int value)
      {
         
           if(troot -> data > value)
           {
                if( troot -> left == NULL)
                {
                     Btree *temp = new Btree();
                     temp -> data = value;
                     troot -> left = temp;
                     temp -> left = NULL;
                     temp -> right = NULL;
                   
                }
                else
                {
               
                    insert(troot->left , value);
                }
           }
           else if(troot -> data < value)
           {
                if( troot -> right == NULL )
                {
                    Btree *temp = new Btree();
                    temp -> data = value;
                    troot -> right = temp;
                    temp -> left = NULL ;
                    temp -> right = NULL;
                   
                }
                else
                {
                 insert (troot->right , value);
                }
           }
           else
           {
               cout << " The given data is already in the the tree " << endl;
           }
      }  
   
      void Btree :: preOrder()
      {
           cout << "Preorder Traversal " << endl;
           preOrder(root);
       }
     
        void Btree :: preOrder(Btree* troot)
       {
            if(troot != NULL)
            {
            cout<<troot -> data<<endl;
            preOrder(troot -> left);
            preOrder(troot -> right);
            }
        }
     
       void Btree :: inOrder()
       {
            cout << " Inorder Traversal " << endl;
            inOrder(root);
        }
       
        void Btree :: inOrder (Btree* troot)
        {
           
             if(troot != NULL)
             {
             inOrder(troot -> left);
             cout << troot -> data <<endl;
             inOrder(troot -> right);
             }
         }
     
     
        void Btree:: postOrder()
        {
             cout << "Postorder Traversal "<<endl;
             postOrder(root);
        }
        void Btree :: postOrder(Btree* troot)
        {
             if(troot != NULL)
             {
             postOrder(troot -> left);
             postOrder(troot -> right);
             cout << troot -> data <<endl;
             }
         }
       
         void Btree :: search(int value)
         {
             
              search(root , value);
             
         }
         void Btree :: search(Btree* troot , int value)
         {
              if(troot -> data == value)
                  {
                           cout <<"The value is present"<<endl;
                  }
             
              else if( (value < troot -> data) && (troot -> left != NULL))
              {
                 
                 
                      search(troot -> left , value);
                 
                 
              }
              else if ( (value > troot -> data) && (troot -> right != NULL) )
              {
         
                       search (troot -> right , data);
                 
              }
              else
              {
                  cout << "The value is not in the Tree " << endl;
              }
             
         }
       
       void Btree :: destroy()
      {
           destroy(root);
      }
     
      void Btree :: destroy(Btree* troot)
      {
           if(troot != NULL)
           {
                    destroy(troot -> left);
                    destroy(troot -> right);
                    delete troot;
           }
      }
     
      int main()
      {
          Btree obj;
          int data = 0;
         obj.insert(40);
         obj.insert(60);
         obj.insert(50);
         obj.insert(33);
         obj.insert(55);
         obj.insert(11);
         obj.insert(10);
       
         obj.preOrder();   // Preorder traversal
         obj.inOrder();    // inorder traversal
         obj.postOrder();  // postorder traversal
       
         cout << " Enter the Data to be searched " << endl;
         cin >> data;
         obj.search(data);  // we can also return the node
         obj.destroy();
         
          cin.get();
          cin.get();
          return 0;
}
                                                                                  BY: SAURABH BHATIA          
     

Monday 8 July 2013

Program to find larger of two number without using relational operator , conditional operator ,if else statements in C/C++...?

I have written the simple code for this without any use of ant bitwise operator....


#include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
    int a = 0 , b = 0 ;
    cout << "Enter two Numbers" << endl;
    scanf("%d",&a);
    scanf("%d",&b);
    int c = a - b ;
    int temp = c + abs(c);
    c = a % b ;
    switch(c)
    {
                case 0 :
                     printf(" a and b are equal \n");
                     break;
                     default:
                             switch(temp)
                             {
                                         case 0:
                                              printf(" a is smaller than b \n");
                                              break;
                                              default:
                                                      printf(" a is greater than b \n");
                             }
    }
    return 0;
}
                                                                                    By: Saurabh Bhatia


                   
                     

Thursday 13 June 2013

Implementation Of Linked List using OOP Concept in C++

Guys I have written the code for Linked List in C++ using OOP concept in a simple way.


#include<iostream>
#include<conio.h>
using namespace std;

class Lis
{
private:
        int data;
        Lis *next , *start;
        int head ;
        public:
        Lis()
        {
                 data = 0;
                 next = NULL;
                 head = 0;
                 start = NULL;
               
               
        }
     
        void add()
        {
           
             Lis *p = new Lis();
             char ch;
             do
             {
             if (head == 0)
             {
                       start = p;
                       p -> next = NULL;
                       head = 1;
                     
             }
             else
             {
                 Lis *tmp = new Lis();
                 p -> next = tmp;
                 tmp ->next = NULL;
                 p = tmp;
               
             }
             cout << "ENTER DATA TO NODE " <<endl;
             cin >> p -> data;
             cout<<"WANT TO ENTER ANOTHER NODE (Y/N) " <<endl;
             cin>>ch;
             }while(ch =='Y');
           
        }
       void addfront()
        {
             Lis *tmp = new Lis();
             cout<<"ENTER DATA " << endl;
             cin>> tmp -> data;
             tmp -> next = start;
             start = tmp;
             display();
             cout<<" NODE SUCCESSFULLY ADDED IN FRONT " << endl;
             cin.get();
           
         }
         void del()
         {
              int loc = 0;
              cout << " ENTER THE LOCATION FROM WHICH YOU WANT TO DELETE NODE " <<endl;
              cin >> loc;
              Lis *tmp;
              tmp = start;
              for(int i = 1 ; i < loc - 1 ; i ++ )
              {
                      tmp = tmp -> next;
              }
              Lis *save = tmp;
              tmp = tmp -> next;
              save -> next = tmp -> next;
              delete(tmp);
              display();
              cout << "NODE SUCCCESSFULLY DELETED " << endl;
              cin.get();
         }
           
         void display()
        {
             Lis *show;
             show  = start;
             while(show)
             {
                        cout<<show -> data <<endl;
                        show = show -> next;
             }
        }
};
int main()
{
    //Lis obj;
    Lis obj;
    int x = 0;
    char ch = 'Y' ;  
    do
    {
    clrscr();
         cout << "Press 1 for making a List " << endl;
         cout << "Press 2 for adding node at beginning " << endl;
         cout << "Press 3 for deleting a node from List " << endl;
         cout << "Press 4 for display the List " << endl;
         cout << "Press 5 to Exit " << endl;
        x = 0;
         cout <<"Enter Choice "<< endl;
        cin >> x;
        switch (x)
        {
               case 1:
                    obj.add();
                    break;
               case 2:
                    obj.addfront();
                    break;
               case 3:
                    obj.del();
                    break;
               case 4:
                    obj.display();
                    break;
               case 5:
                    ch = 'N';
                    break;
               default:
                       cout << " Enter Valid Choice " << endl;
        }
        }while (ch == 'Y' );
    return 0;
}      
 
                                                                                                         By: Saurabh Bhatia
           

Monday 21 January 2013

TopCoder SRM 567 250 Problem


The Ninja Turtles often battle the Foot Clan ninjas. The Turtles celebrate each victory with a pizza party. The amount of pizza they eat depends on the number of opponents they have defeated. Denote the number of defeated opponents as N. Three of the four Turtles have a moderate appetite and only consume floor(N / K) pizzas each. The fourth Turtle is always hungry and eats floor(N / 3) pizzas.

You are given ints P and K, where P is the total number of pizzas the Turtles ate after a battle. If there exists at least one value of N such that after defeating N opponents the Turtles would eat exactly P pizzas at the party, return the smallest such N. Otherwise, return -1.

I have solved this in a simple way...check it out...:)

#include<iostream>
#include<cmath>
using namespace std;
class NinjaTurtles
{
public:
int countOpponents(int P , int K )
{
int x = 1;
int temp = 0 , temp1 = 0;
int y = (int)((3*K*P) / ( K + 3) );
cout<<y<<endl;
while( (x < K) || (x < 3))
{
            temp = (int)floor(x/K);
temp1 = (int)floor(x/3);
            temp = temp * 3;
            if((temp + temp1) == P)
{
return x;
}  
x++;
            }
while(x <= y)
{
            temp = (int)floor(x/K);
temp1 = (int)floor(x/3);

temp = temp * 3;
if((temp + temp1) == P)
{
return x;
}
x++;
}
return -1;
}
};
                                                                               BY:  SAURABH BHATIA



Sunday 13 January 2013

ekSMS.com and YourStory.in Hiring Challenge


Although this is giving a wrong answer according to the compiler of hackers earth but according to me it is appropriate solution for the problem. so if you find any bug in this plz do comment on it.

PROBLEM:
There are many ways to order a list of integers from 1 to n. For example, if n = 3, the list could be : [3 1 2].
But there is a special way to create another list from the given list of integers. In this list, position of integer i is the i-th number in the given list. So, the above list will be written as: [2 3 1]. This list is called inverse list. Now there exists some list whose inverse list is identical. For example, inverse list of [1 2 3] is same as original list. Give a list of integers you have to determine whether the list is inverse or not.
The input contains several test cases. The first line is the number of test cases t (1 <= t <= 100) . The first line of each test case contains an integer n (1 <= n <= 100000). Then a list of the integers 1 to n follows in the next line.
SOLUTION:   I have write this simple code for this problem.

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
class saurabh
{
      public:
             void sau(int t)
             {
                  long n = 0;
                  long tmp = 0;
                 
                  for(int l = 0 ; l < t ; l ++)
                  {
                  scanf("%ld",&n);
                  vector <long> num(n);
                    vector<long>cm(n);
                   tmp = 0;
                    for(long i = 0 ; i < num.size() ; i++)
                    {
                             scanf("%ld",&tmp);
                             num[i] = tmp;
                             cm[tmp-1] = i+1;
                    }
                    if(cm == num)
                    {
                          printf("inverse");
                    }
                    else
                    {
                        printf("not inverse");
                    }
                    num.clear();
                    cm.clear();
                  }
             }
             };
             int main()
             {
                 saurabh obj;
                 int t = 0;
                 scanf("%d",&t);
                 obj.sau(t);
                 cin.get();
                 return 0;
             }
                                                                    BY: SAURABH BHATIA            


Friday 11 January 2013

PERMUTATION & COMBINATION PROBLEM EASY SOLUTION

AS YOU HAVE SEEN MANY PROBLEMS IN VARIOUS CODING COMPETITION LIKE TOPCODER , CODECHEF , CODEFORCE FOR OUTPUT THE NUMBER OF OCCURRENCES OF THE VARIOUS ITEMS TOGETHER OR DIFFERENTLY. AS IN THIS CODE I HAVE TAKEN TWO ITEMS C AND D AND THIS PROGRAMME WILL OUTPUT THE COMBINATIONS OF THESE TWO IN 5 PLACES. THIS IS A SIMPLE CODE WRITTEN BY ME.




#include<iostream>
#include<string>
using namespace std;
int main()
{
    int numLength = 5;
    string tmp ;
    int x = 0;
    for(int k = 0 ; k <(1<<numLength); k++) // it forms the combinations
    {
   
             x = k;
            for(int i = 0 ; i <numLength ; i++)
            {
                   
                    if( (x & 1) == 1)
                    tmp+= 'c';
                    else
                    tmp+= 'd';
                    x>>= 1; // as it right shift the value so the combination occurs
                   
                    }
           cout<<k<<'\t'<<tmp<<endl;
           tmp = "";
           }
           cout<<tmp<<endl;
           x = 5;
           cout<<(x>>1)<<endl;
           cout<<(x&1)<<endl;
                       
                   
                         
    cin.get();
    return 0;
}

                                                                   BY : SAURABH BHATIA
           

TIC TAC TOE GAME IN ANDROID

I HAVE MADE A SIMPLE CODE FOR MAKING TIC TAC TOE GAME IN ANDROID .FIRST YOU HAVE TO DESIGNED BUTTONS BY USING ECLIPSE THAN HAVE TO DO CODING IN ANDROID.


package saurabh.rox.namespace;   // AS MY NAME OF THE PACKAGE IS SAURABH

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GameActivity extends Activity {
/** Called when the activity is first created. */
int count = 1;
int flag = 0;
Button[] A = new Button[9];
final Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("TicTacToe");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("TicTacToe");
alertDialog
.setMessage("WELCOME TO THE 'TicTacToe' GAME \nMADE BY SAURABH OF (CSE 2nd YEAR)");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Play",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();

}
});
AlertDialog alertDia = alertDialog.create();
alertDia.show();

A[0] = (Button) findViewById(R.id.button1);
A[1] = (Button) findViewById(R.id.button2);
A[2] = (Button) findViewById(R.id.button3);
A[3] = (Button) findViewById(R.id.button4);
A[4] = (Button) findViewById(R.id.button5);
A[5] = (Button) findViewById(R.id.button6);
A[6] = (Button) findViewById(R.id.button7);
A[7] = (Button) findViewById(R.id.button8);
A[8] = (Button) findViewById(R.id.button9);

A[0].setText("");
A[1].setText("");
A[2].setText("");
A[3].setText("");
A[4].setText("");
A[5].setText("");
A[6].setText("");
A[7].setText("");
A[8].setText("");

}

public void set() {
A[0].setText("");
A[1].setText("");
A[2].setText("");
A[3].setText("");
A[4].setText("");
A[5].setText("");
A[6].setText("");
A[7].setText("");
A[8].setText("");
count = 1;
A[0].setClickable(true);
A[1].setClickable(true);
A[2].setClickable(true);
A[3].setClickable(true);
A[4].setClickable(true);
A[5].setClickable(true);
A[6].setClickable(true);
A[7].setClickable(true);
A[8].setClickable(true);

}

// FOR DIALOG BOX

public void dialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("TicTacToe");
if (count >= 10) {
alertDialogBuilder
.setMessage("THERZ A TIE BETWEEN THE PLAYERS \nCLICK 'Yes' TO PLAY AGAIN AND 'No' TO EXIT.");
} else if (count % 2 == 0) {

alertDialogBuilder
.setMessage("O HAS WON THE GAME \nCLICK 'Yes' TO PLAY AGAIN AND 'No' TO EXIT.");
} else {
alertDialogBuilder
.setMessage("X HAS WON THE GAME \nCLICK 'Yes' TO PLAY AGAIN AND 'No' TO EXIT.");
}
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),
"New Game Has Been Started \nAll The Best.",
Toast.LENGTH_SHORT).show();

}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),
"Thanks For Playing The Game",
Toast.LENGTH_SHORT).show();
GameActivity.this.finish();

}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
// this is just for showing info for a second Toast.makeText(this,
// "This is the Toast message", Toast.LENGTH_LONG).show();

}

// FOR CHECKING

public void check() {
flag = 0;
if (count >= 10) {
dialog();
set();

}
for (int i = 0; i < 3; i++) {
if (A[i].getText() != "") {
if (A[i].getText().equals(A[i + 3].getText())
&& A[i].getText().equals(A[i + 6].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}
}
}
}
for (int j = 0; j < 7; j = j + 3) {
if (A[j].getText() != "") {
if (A[j].getText().equals(A[j + 1].getText())
&& A[j].getText().equals(A[j + 2].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}

}
}
}
if (A[0].getText() != "") {
if (A[0].getText().equals(A[4].getText())
&& A[0].getText().equals(A[8].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}

}
}
if (A[2].getText() != "") {
if (A[2].getText().equals(A[4].getText())
&& A[2].getText().equals(A[6].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}

}

}

}

public void onMyButtonClick(View view) {

if (count % 2 == 0) {
((Button) view).setText("X");
((Button) view).setClickable(false);

} else {
((Button) view).setText("O");
((Button) view).setClickable(false);
}
count++;
check();

}

}

                                                      BY : SAURABH BHATIA