Sunday, April 15, 2012

Program 26TH in C++

PROBLEM STATEMENT :

Write a function isValid(int sudoko[][]) to chec whether a given sudoko solution is valid or not.

SOLUTION :

#include <cstdio>
#include <iostream.h>

using namespace std;

//initializing the count array with val.

void reinit(int c[9],int val)
{
for(int i=0; i<9; i++)
{
c[i]=val;
}
}

//checking whether all calues in count array are set to zero or not

bool check (int c[9])
{
for(int i=0; i<9; i++)
{
if(c[i] != 0)
{
return false;
}
}
return true;
}

//function isValid

bool isValid(int a[][9])
{
int i=0;
int j=0;
int k=0;
int p=0;
int count[]={1,1,1,1,1,1,1,1,1};

//checking first 9 blocks for validity

while(k < 9)
{
for(i=k; i<(k+3); i++)
{
for(j=p; j<(p+3); j++)
{
if(a[i][j] > 0 && a[i][j] <10)
{
count[a[i][j]-1]--;

if(count[a[i][j]-1] < 0)
{
return false;
}
}
else
{
return false;
}
}
}
if(check(count))
{
reinit(count,1);
}
else
{

return false;
}

p=p+3;
if(p == 9)
{
p=0;
k=k+3;
}

}
reinit(count,1);
//checking rows for validity

for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if(a[i][j] > 0 && a[i][j] < 10)
{
count[a[i][j]-1]--;
if(count[a[i][j]-1] < 0)
{
return false;
}
}
else
{
return false;
}

if(j == 8 && i!=8)
{
if(check(count))
{
reinit(count,1);
}
else
{
return false;
}
}

}
}
if(check(count))
{
reinit(count,1);
}
else
{
return false;
}

//checking columns for validity

for(j=0 ; j<9 ; j++)
{
for(i=0; i<9; i++)
{
if(a[i][j] > 0 && a[i][j] < 10)
{
count[a[i][j]-1]--;

if(count[a[i][j]-1] < 0)
{
return false;
}
}
else
{
return false;
}
if(i == 8 && j != 8)
{
if(check(count))
{
reinit(count,1);
}
}

}
}

if(check(count))
{
return true;
}
else
{
return false;
}



}

int main()
{

int a[9][9]={{2,4,8,3,9,5,7,1,6},{5,7,1,6,2,8,3,4,9},{9,3,6,7,4,1,5,8,2},{6,8,2,5,3,9,1,7,4},{3,5,9,1,7,4,6,2,8},{7,1,4,8,6,2,9,5,3},{8,6,3,4,1,7,2,9,5},{1,9,5,2,8,6,4,3,7},{4,2,7,9,5,3,8,6,1}};

if(isValid(a))
{
cout<<"true";
}
else
{
cout<<"false";
}
}

Program 25TH in C++


PROBLEM STATEMENT :

If you are given an input array of chars 1 2 3 4 a b c d
output should be 1 a 2 b 3 c 4 d
No Buffers should be used
variables are permissible.

SOLUTION :

#include <iostream.h>
#include <cstdio>

int main()
{

char a1[8]={'1','2','3','4','a','b','c','d'};

int strlen=sizeof(a1)/sizeof(char);
int i=0;
int j=0;

for(i=0; i<strlen; i++)
{

if(a1[i]<48 || a1[i]>57)
{
j=i;
break;
}

}


for(i=1; j<strlen; i+=2)
{

int k=j++;
char temp = a1[k];
while(k > i)
{
a1[k]=a1[k-1];
k--;

}
a1[k]=temp;
}


cout<<"\n\no/p : ";
for(i=0; i<strlen; i++)
{
cout<<a[i]<<" ";
}

}


Program 24TH in C++


PROBLEM STATEMENT :

Find number in a circularly sorted array.

SOLUTION :

#include <iostream.h>
#include <cstdio>

using namespace std;

int main()
{
int target;
int a1[]={6,7,8,9,1,2,3};
int a[]={3,1,9,8,7,6,5};
cout<<"\n\nEnter number you want to search : ";
cin>>target;


int min=0;
int max=sizeof(a)/sizeof(int)-1;
while( min <= max && min >=0 && max <=sizeof(a)/sizeof(int)-1)
{
int mid =(min+max)/2;
if(a[mid] == target)
{
cout<<"Found at index "<<mid;
exit(0);
}
else if(a[mid] >= a[min])
{
if(a[min] <= target && target <= a[mid])
{
max=mid-1;
}
else
{
min=mid+1;
}
}
else if(a[mid] <= a[max])
{
if(a[mid] <= target && target <= a[max])
{
min=mid+1;
}
else
{
max=mid-1;
}
}

}
}


Program 23RD in C++ VERSION 2


PROBLEM STATEMENT :

What is the diameter of a tree?

SOLUTION :

#include <iostream.h>
#include <cstdio>

using namespace std;

int flag = 1;

struct node
{
int data;
node *left;
node *right;
};

node* node(int n)
{

struct node* m = (struct node*)malloc(sizeof(struct node));

m->data=n;
m->left=NULL;
m->right=NULL;

return m;
}

int max(int a, int b)
{

return a>b?a:b;

}

int diameter(struct node* n, int* height)
{

int lh=0;
int rh=0;
int ld=0;
int rd=0;

if(n == 0)
{
*height=0;
return 0;
}

if(n->left == 0 && flag == 1)
{
n=n->right;
return diameter(n,height);
}
else if (n->right == 0 && flag == 1)
{
n=n->left;
return diameter(n,height);
}
else
{
flag=0;
}

ld = diameter (n->left,&lh);
rd = diameter (n->right,&rh);

*height=max(lh,rh)+1;

return (max(lh+rh+1,max(ld,rd)));
}

int main()
{

struct node* root = node(0);
root->right=node(1);
root->right->left=node(7);
root->right->right=node(2);
root->right->right->right=node(3);
root->right->right->right->left=node(4);
root->right->right->right->right=node(5);

int height;

cout<<diameter(root,&height);

}

Program 23RD in C++ VERSION 1


PROBLEM STATEMENT :

What is the diameter of a tree?

SOLUTION :

#include <iostream.h>
#include <cstdio>

using namespace std;

int flag=1;

struct node
{
int data;
node* left;
node* right;
};

int max(int a, int b)
{

return (a > b ? a : b);

}

int height(node* n)
{

if(n == 0)
{
return 0;
}
else
{
return 1+max(height(n->left),height(n->right));
}

}

node* node(int val)
{

struct node* m=(struct node*)malloc(sizeof(struct node));
m->data=val;
m->left=NULL;
m->right=NULL;

return m;
}

int diameter(struct node* n)
{
if(flag == 1 && n->left == 0)
{
n=n->right;
return diameter(n);
}
else if (flag == 1 && n->right == 0)
{
n=n->left;
return diameter(n);
}
else
{
flag = 0 ;
}
if(n == 0)
{
return 0;
}
int lh = height(n->left);
int rh = height(n->right);

int ld = diameter(n->left);
int rd = diameter(n->right);
return(max(lh+rh+1,max(ld,rd)));
}

int main()
{

struct node* root;
root = node(0);
root->left = node(1);
root->right = node(7);
root->right->right = node(4);
//root->right->left = node(7);
root->right->right->right = node(3);
//root->right->right->right->left = node(8);
root->right->right->right->right = node(2);

cout<<diameter(root);
}


Program 22ND in C++

PROBLEM STATEMENT :


How to compute A^n where n<1 million

Write code...

SOLUTION :

#include <iostream.h>
#include <cstdio>

using namespace std;

long double power(long double num, long int pow)
{

int flag = pow >=0 ? 0 : 1;

//long double one= 1.0F;
if(flag != 0)
{
pow*=-1;
num=1/(num);
}
if(pow == 0L)
{
return 1;
}
else if(pow == 1L)
{
return num;
}
else if(pow % 2 == 0)
{
return power(num,pow/2)*power(num,pow/2);
}
else
{
return power(num,pow/2)*power(num,pow/2)*num;
}
}
int main()
{

cout<<power(2,12345)<<endl;
cout<<power(2,51)<<endl;
cout<<power('\0','\0')<<endl;
cout<<power(2,0)<<endl;
cout<<power(2,-2);

}


Program 21ST in C++ VERSION 2

PROBLEM STATEMENT :

Find the longest palindrome in a given string

SOLUTION :

//solves in o(n^2)

#include <cstdio>
#include <iostream.h>
#include <string.h>

using namespace std;

string find(string s2,int i, int j)
{
int l=i;
int r=j;
int length=s2.length();

while(i>=0 && j<length && s2[i] == s2[j])
{
i--;
j++;

}

return s2.substr(i+1,j-i-1);

}
void palin(string s)
{

int l=s.length();

if(s.length() == 0)
{
cout<<" ";
}

string longest = s.substr(0,1);

for(int i=0; i
{
string s1=find(s,i,i);
if(s1.length() > longest.length())
{
longest=s1;
}

s1=find(s,i,i+1);
if(s1.length() > longest.length())
{
longest=s1;
}
}

cout<<"LONGEST PALINDROME IS : "<<longest;
}

int main()
{

string s;
cout<<"Enter a string : ";
getline(cin,s,'\n');
palin(s);

}

Program 21ST VERSION 1 in C++

PROBLEM STATEMENT :

Find the longest palindrome in a given string

SOLUTION :

//solves in o(n^3)

#include <cstdio>
#include <iostream.h>
#include <string.h>

using namespace std;

string strrev(string s)
{
int n=s.length()/2;
char temp;

for(int i=0; i<n; i++)
{
temp = s[i];
s[i]=s[s.length()-i-1];
s[s.length()-i-1]=temp;
}
cout<<" "<<s;
return s;

}

string longpalin(string s1)
{
int pos=0;
int len=0;

if(s1.length() == 0)
{
return "";
}

for( int i=0; i<s1.length(); i++)
{
for( int j=i+1; j<=s1.length()-i ;j++)
{
cout<<"\n\nstring : "<<s1.substr(i,j);
if(s1.substr(i,j) == (strrev(s1.substr(i,j))) && j>len)
{
pos=i;
len=j;
}
}
}

return s1.substr(pos,len);

}


int main()
{
string s;

cout<<"\n\nEnter a string : ";
getline(cin,s,'\n');

cout<<"\n\nLongest Palindrome is : "<<longpalin(s);

}

Program 20TH in C++

PROBLEM STATEMENT :

Check whether an integer is a palindrome.

SOLUTION :

#include
#include

using namespace std;

int main()
{
int num;
cout<<"Enter a number to check if its a palindrome : ";
cin>>num;

int div=10;
int x;
int q;
int r;

if(num <0)
{
cout<<"\n\nNot a Palindrome\n\n";
exit(0);
}
while(num/div >=10)
{
div*=10;
}

while(num != 0)
{

q=num/div;
r=num%10;

if( q != r )
{
cout<<"\n\nNot a palindrome\n\n";
exit(0);
}
else
{
num=(num%div)/10;
div /= 100;
}
}
cout<<"\n\nA Palindrome\n\n";
}

Program 19TH in C++

PROBLEM STATEMENT :

Give an algorithm to generate random numbers between 1 and 2.

SOLUTION :

#include <cstdio>
#include <iostream.h>
#include <cstdlib>
#include <ctime>

float range(float a, float b)
{

srand(time(0));
float x = (float)rand()/RAND_MAX;
x*=(b-a);
x+=a;
return x;

}
int main()
{


cout<<" "<<range(1.0,2.0);

}

Program 18TH in C++

PROBLEM STATEMENT :


calculate average of the values in a binary tree?


SOLUTION :




#include <cstdio>
#include <iostream.h>
#include <queue.h>


using namespace std;


class node
{
public:
int data;
node* left;
node* right;
};


//finding average by breadth first traversal


void traverse(node * root)
{
queue q;
q.push(root);
int current_count=1;
int next_count=0;
int tot=0;
int sum=0;


while(!q.empty())
{
node* p1 = q.front();
q.pop();
current_count--;
cout<<data<<" ";
//tot keeps the count of no of nodes popped
tot++;


//adding values of popped nodes


sum+=p1->data;


if(p1->left != NULL)
q.push(p1->left);
next_count++;


if(p1->right != NULL)
q.push(p1->right);
next_count++;


if(current_count == 0)
{
current_count=next_count;
next_count=0;
cout<<endl;
}
}


cout<<"\n\nAverage of the "<<tot<<" node values in the binary tree is : "<<sum/(float)tot;


}
int main()
{


node Node[10];
for(int i=0; i<10; i++)
{
Node[i].data=i*2;
Node[i].left=NULL;
Node[i].right=NULL;
}


Node[0].left = &Node[1];
Node[0].right= &Node[2];
Node[1].left = &Node[3];
Node[1].right= &Node[4];
Node[2].left = &Node[5];
Node[2].right= &Node[6];
Node[3].left = &Node[7];
Node[3].right= &Node[8];
Node[4].left = &Node[9];
traverse(Node);


}


Program 17TH in C++

PROBLEM STATEMENT :

Return all factorials of given integer. Enhance your approach by avoiding linear traversing.

SOLUTION :

#include <cstdio>
#include <iostream.h>
#include <set.h>
#include <math.h>

using namespace std;

int main()
{

int num;
int i;
int flag=0;

cout<<"enter num : ";
cin>>num;

set factors;
set :: iterator it;

if(num < 0)
{
num*=-1;
}

for(i=1; i<=sqrt(num); i++)
{

if( num % i == 0 )
{
if(factors.find(i)==factors.end())
{
factors.insert(i);
factors.insert(num/i);
factors.insert(-i);
factors.insert(-num/i);

}
else
{
break;
}
}

}

cout<<"Factors are :";
for(it=factors.begin(); it!=factors.end(); it++)
{
cout<<" "<<*it;
}

}

Program 16TH VERSION 2 in C++

PROBLEM STATEMENT :

Calculate number of zeros in a given integer in binary format.

SOLUTION :


#include <iostream.h>
#include <cstdio>

using namespace std;

void count(int num)
{

int count=0;

while(num>0)
{
count+=!(num & 1);
num=num>>1;
}

cout<<"\n\nThe number of zeros in the given string are : "<<count<<endl;
}

int main()
{

int number;

count (3);
count(0);
count('\0');

}

Program 16TH in C++

PROBLEM STATEMENT :

Calculate number of zeros in a given integer.

SOLUTION :


#include <cstdio>
#include <iostream.h>

using namespace std;

int main()
{
int number;
int count=0;

cout<<"Enter an integer : ";
cin>>number;

while(number > 0)
{
count=count+(!(number%10)?1:0);
number=number/10;
}

cout<<"The number of zeros in the given number are : "<<count;


}

Thursday, April 12, 2012

Program 15TH in C++ (VERSION 2)

PROBLEM STATEMENT :

Matrix with rows and column sorted. Find a particular element.

NOTE:

Works for arrays where the last element of previous row in 2d matrix is less than first element of next row with time complexity l0g m +log n

SOLUTION :

#include <iostream.h>
#include <cstdio>

using namespace std;

void find(int arr[][4], int m, int n, int target, int &row, int &col)
{
int a1d[16];
int first;
int last;


for(int x=0; x
{
a1d[x]=arr[x/m][x%n];
}


first=0;
last=15;
while(first>=0 && last<16 && first <= last)
{
int mid = (first+last)/2;
if(a1d[mid]==target)
{
row=mid/m;
col=mid%n;
return;
}
else if(a1d[mid] > target)
{
last=mid-1;
}
else
{
first=mid+1;
}
}

}

int main()
{
int a2d[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int val;

cout<<"\n\nEnter the element you want to find : ";
cin>>val;

int row = -1;
int col = -1;

find(a2d,4,4,val,row,col);

if(row !=-1 && col !=-1)
{
cout<<"\n\nElement is at row : "<<row <<" and column : "<<col<<endl;
}
else
{
cout<<"\n\nElement not present in the array\n\n";
}
}

Program 15TH in C++ (VERSION 1)

PROBLEM STATEMENT :

Matrix with rows and column sorted. Find a particular element.

NOTE:

It works for all kind of arrays with O(m+n) time complexity.

SOLUTION:

#include <iostream.h>
#include <cstdio>

using namespace std;

void find(int a2d[][4], int m, int n, int target)
{
int i=0;
int j=n-1;
int flag=1;

while(i>=0 && i<m && j >=0 && j<n)
{
if(a2d[i][j]==target)
{
flag=0;
cout<<"Element "<<target << "exists at " << " row : "<<i<<" and column : "<<j<<endl<<endl;
j--;
}
else if(a2d[i][j] > target)
{
j--;
}
else
{
i++;
}

}
if(flag == 1)
{
cout<<"\n\n Element is not found in the given array \n\n";
}

}

int main()
{
int row=0;
int col=0;
int a2d[][4]={{1,2,3,5},{2,5,6,7},{3,6,11,13},{4,10,15,18}};
find(a2d,4,4,6);


}

Wednesday, April 11, 2012

Program 14TH in C++

PROBLEM STATEMENT :

there two article:A ,B,which is very large. get three or more successive words in A,to find if it appears in B ,and count the times. For example , 'book' 'his' 'her' appear in A ,how many times it appears in B?

SOLUTION:

#include <iostream.h>
#include <cstdio>
#include <map.h>
#include <string.h>

using namespace std;

void findArr(string a, string out[])
{
string s="";
int j=0;
for(int i=0; i<=a.length(); i++)
{
if(a[i]!=' ' && a[i]!='\0')
{
s+=a[i];
}
else
{
out[j]=s+'\0';
j++;
s="";
}
}

}

int size( string a)
{
int count =0;
for(int i=0; i
{
if(a[i] == ' ')
count++;
}
return count+1;
}

int main()
{
string a="hello bye tea coffee";
string b="hello hello tea";

string arr1[size(a)];
string arr2[size(b)];

findArr(a,arr1);
findArr(b,arr2);

map<string,int> store;
for(int i=0; i<sizeof(arr1)/sizeof(string); i++)
{
store.insert(pair(arr1[i],0));
}

map<string,int> :: iterator it;
for(int i=0; i
{
it=store.find(arr2[i]);
if(it!=store.end())
{
store[arr2[i]]+=1;
}

}

for(int i=0; i<sizeof(arr1)/sizeof(string); i++)
{
cout<<"\nString "<<arr1[i] << " occurs in B " << store[arr1[i]] << " number of times"<< endl;
}

}

Program 13TH in C++

PROBLEM STATEMENT :

Return true if two trees are same

SOLUTION:

#include <iostream.h>
#include <cstdio>
#include <queue.h>
#include <vector.h>

using namespace std;

struct node
{
int data;
vector<node*> children;
};

//checking by level order traversal

bool check_tree(node* n, node* m)
{
queue<node*> q1;
queue<node*> q2;
int curr_count=1;
int next_count=0;

q1.push(n);
q2.push(m);

while(!q1.empty() && !q2.empty())
{
node* val1=q1.front();
node* val2=q2.front();
q1.pop();
q2.pop();
curr_count--;
if(val1->data == val2->data)
{
int size1=val1->children.size();
if(size1 == val2->children.size())
{
for(int i=0; i <size1; i++)
{
q1.push(val1->children[i]);
q2.push(val2->children[i]);
next_count++;
}
if(curr_count == 0)
{
curr_count=next_count;
next_count=0;
}
}
else
{
return false;
}
}
else
{
return false;
}

}
return true;

}

int main()
{
node Node[10];
node Node1[10];

for(int i=0; i<10; i++)
{
Node[i].data=i*2;
Node1[i].data=i*2;
}

//arranging childrens in first tree

Node[0].children.push_back(&Node[1]);
Node[0].children.push_back(&Node[2]);
Node[1].children.push_back(&Node[3]);
Node[1].children.push_back(&Node[4]);
Node[1].children.push_back(&Node[5]);
Node[2].children.push_back(&Node[6]);
Node[2].children.push_back(&Node[7]);
Node[2].children.push_back(&Node[8]);
Node[3].children.push_back(&Node[9]);

//arranging children in second tree

Node1[0].children.push_back(&Node1[1]);
Node1[0].children.push_back(&Node1[2]);
Node1[1].children.push_back(&Node1[3]);
Node1[1].children.push_back(&Node1[4]);
Node1[1].children.push_back(&Node1[5]);
Node1[2].children.push_back(&Node1[6]);
Node1[2].children.push_back(&Node1[7]);
Node1[3].children.push_back(&Node1[8]);
Node1[3].children.push_back(&Node1[9]);

bool out=check_tree(Node,Node1);

if(out == true)
cout<<"\n\n Trees are same\n\n";
else
cout<<"\n\n Trees are not same\n\n";
}