Sunday, 9 July 2017

Common divisor

#include <iostream>
using namespace std;

int main()
{
   int a,b,c[50],d[50],count=0;
   cin>>a>>b;
   for(int i=0;i<50;i++){
       if(a%(i+1)==0){
           c[i]=i+1;
       }
       
   }
    for(int i=0;i<50;i++){
       if(b%(i+1)==0){
           d[i]=i+1;
       }
       
   }
   for(int i=0;i<50;i++){
       if(c[i]==d[i]){
           cout<<c[i]<<endl;
       
   }}
  
}

Friday, 7 July 2017

Palindrome

#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cin>>s;
int b=0;
for(int i=0;i<s.size();i++){if(s[i]!=s[s.size()-i-1])

{b=1;
break;}
}
if(b==0){
    cout<<"yes";
}
else cout<<"N0";
    }
    

Graph Theory

Graph Theory

Adjacency matrix
An adjacency matrix is a VxV binary matrix A. Element Ai,j is 1 if there is an edge from vertex i to vertex j else Ai,j is 0.
Note: A binary matrix is a matrix in which the cells can have only one of two possible values - either a 0 or 1.
The adjacency matrix can also be modified for the weighted graph in which instead of storing 0 or 1 in Ai,j, the weight or cost of the edge will be stored.
In an undirected graph, if Ai,j = 1, then Aj,i = 1. In a directed graph, if Ai,j = 1, then Aj,i may or may not be 1.
Adjacency matrix provides constant time access (O(1) ) to determine if there is an edge between two nodes. Space complexity of the adjacency matrix is O(V2).
The adjacency matrix of the following graph is:

i/j1 2 3 4
1 : 0 1 0 0
2 : 0 0 0 1
3 : 1 0 0 1
4 : 0 1 0 0
enter image description here


code for adjacency matrix

#include<iostream>
using namespace std;
int main(){

    int x,y,node,edge;

    cin>>node>>edge;

    int a[10][10]={0};

    for(int i=0;i<edge;i++){

        cin>>x>>y;

        a[x][y]=1;
        
    }
  
}

TEST YOUR UNDERSTANDING
Edge Existence
You have been given an undirected graph consisting of N nodes and M edges. This graph can consist of self-loops as well as multiple edges. In addition , you have also been given Q queries. For each query , you shall be given 2 integers A and B. You just need to find if there exists an edge between node A and node B. If yes, print "YES" (without quotes) else , print "NO"(without quotes).
Input Format:
The first line consist of 2 integers N and M denoting the number of nodes and edges respectively. Each of the next M lines consist of 2 integers A and B denoting an undirected edge between node A and B. The next line contains a single integer Q denoting the number of queries. The next Line contains 2 integers A and B denoting the details of the query.
Output Format
Print Q lines, the answer to each query on a new line.
Constraints:
1N103
1M103
1A,BN
#include<iostream>
using namespace std;
int main(){

    int x,y,node,edge;

    cin>>node>>edge;

    int a[10][10]={0};

    for(int i=0;i<edge;i++){

        cin>>x>>y;

        a[x][y]=1;
        
    }
  int r,p,q;
  cin>>r;
  for(int i=0;i<r;i++){
      cin>>p>>q;
      if(a[p][q]==1){
          cout<<"YES"<<endl;
          
      }
      else cout<<"NO"<<endl;
      
  }

}

Bubble Sorting

#include <iostream>

using namespace std;

void swap(int &a,int &b){

    int r;

    r=a;

    a=b;

    b=r;}

int main()

{

  int n;

  cin>>n;

  int a[n];

  for(int i=0;i<n;i++){

      cin>>a[i];

  }

  for(int i=0;i<n;i++){

      for(int j=0;j<n;j++){

          if(a[j]>a[j+1])

          swap(a[j],a[j+1]); }

  }

  for(int i=0;i<n;i++){

      cout<<a[i]<<endl;}

}

Thursday, 6 July 2017

Lonely Integer

Consider an array of  integers, , where all but one of the integers occur in pairs. In other words, every element in  occurs exactly twice except for one unique element.
Given , find and print the unique element.
Input Format
The first line contains a single integer, , denoting the number of integers in the array. 
The second line contains  space-separated integers describing the respective values in .
Constraints
  • It is guaranteed that  is an odd number.
  • , where .
Output Format
Print the unique number that occurs only once in  on a new line.
Sample Input 0
1
1
Sample Output 0
1
Explanation 0 
The array only contains a single , so we print  as our answer.
Sample Input 1
3
1 1 2
Sample Output 1
2
Explanation 1 
We have two 's and one . We print , because that's the only unique element in the array.
Sample Input 2
5
0 0 1 2 1
Sample Output 2
2
Explanation 2 
We have two 's, two 's, and one . We print , because that's the only unique element in the array.
Solution:
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int a[1000]={0},b;
for(int i=0;i<n;i++){
    cin>>b;
    a[b]++;
}
for(int i=0;i<1000;i++){
if(a[i]==1)
    cout<<i;}
}
   

Dynamic programming problem

Given a binary matrix, find out the maximum size square sub-matrix with all 1s. For example, consider the below binary matrix. Please...