mani 4m sklm
BAN USERif a==0 and b==0 then u have to return NaN
 mani 4m sklm May 03, 2014i+j=8 line and i+j = temp are wrong.They should be == instead of .i,j are not declared.Except that,logic is correct
 mani 4m sklm January 29, 2014for(i=0;i<n/2;i++)
check if any number sq is that number
greedy algo is for fractional knapsack
dp is for 01 knapsack
It is basically dp problem.I thought of following recursion but I cant optimize it further
a[i]=min{a[i5]*2,3,5;a[i4]*2,3,5;a[i3]*2,3,5;a[i2]*2,3,5;a[i1]*2,3,5} and that num>a[i]
@vankasundeep O(nlogn).
@Dumbo can u please explain for what type of problems shall we use suffix array
and cant we do it using dynamic programing
@Nascent Is it necessary that we have to go to hill only via horse?
 mani 4m sklm July 01, 2013Isn't answer for the question is 1,3,4,5,6
 mani 4m sklm June 30, 2013can't we sort it? O(nlogn)
 mani 4m sklm June 30, 2013can't we use median of medians here?complexity O(kn)
 mani 4m sklm June 30, 2013#include <stdio.h>
#define MAX 5
#define MAX1 4
int main(int argc, char const *argv[])
{
int a[MAX][MAX1];
int i,j;
for (i = 0; i < MAX; ++i)
{
for (j = 0; j < MAX1; ++j)
{
a[i][j]=rand()%2;
printf("%d ",a[i][j] );
}
printf("\n");
}
printf("\n");
int count=0;
for (i = 0; i < MAX; ++i)
{
for (j = 0; j < MAX1; ++j)
{
if(a[i][j]==1)
{
if(j!=MAX1)
{
switch(a[i][j+1])
{
case 1:
a[i][j]=a[i][j+1]=2;
count++;
break;
case 2:
printf("some error here %d %d",i,j);
break;
case 3:
a[i][j]=2;
a[i][j+1]=4;
count++;
break;
case 4:break;
}
}
if(i!=MAX)
{
switch(a[i+1][j])
{
case 1:
a[i][j]=a[i+1][j]=3;
count++;
break;
case 2:
a[i][j]=3;
a[i+1][j]=4;
count++;
break;
case 3:
printf("some problem %d %d\n",i,j);
break;
case 4:
break;
}
}
}
else if(a[i][j]==2)
{
if(j!=MAX1)
{
switch(a[i][j+1])
{
case 1:
a[i][j]=a[i][j+1]=2;
break;
case 2:
printf("some error here %d %d",i,j);
break;
case 3:
a[i][j]=2;
a[i][j+1]=4;
count++;
break;
case 4:break;
}
}
if(i!=MAX)
{
switch(a[i+1][j])
{
case 1:
a[i][j]=4;
a[i+1][j]=3;
count++;
break;
case 2:
// a[i][j]=3;
// a[i+1][j]=4;
break;
case 3:
a[i][j]=2;
a[i][j+1]=4;
count++;
break;
case 4:
break;
}
}
}
else if(a[i][j]==3)
{
if(j!=MAX1)
{
switch(a[i][j+1])
{
case 1:
a[i][j]=4;
a[i][j+1]=3;
count++;
break;
case 2:
printf("some error here %d %d",i,j);
break;
case 3:
break;
case 4:break;
}
}
if(i!=MAX)
{
switch(a[i+1][j])
{
case 1:
a[i][j]=4;
a[i+1][j]=3;
break;
case 2:
// a[i][j]=3;
// a[i+1][j]=4;
break;
case 3:
a[i][j]=2;
a[i][j+1]=4;
break;
case 4:
break;
}
}
}
printf("%d ",a[i][j] );
}
printf("\n");
}
printf("%d \n",count );
return 0;
}

mani 4m sklm
June 26, 2013 first iterate from back.First check the sum of the all numbers which is the length of the array.And in the next iteration expand the digits if the expansion is going to overwrite the string then do it at that place else do it at the right place.For the next iteration remove the spaces.O(n)
 mani 4m sklm June 26, 2013#include <stdio.h>
#include <string.h>
void fun(char *s,char *s1);
int main(int argc, char const *argv[])
{
int i=0;
char str[10]="1",str1[10];
for (i = 0; i < 10; ++i)
{
fun(str,str1);
strcpy(str,str1);
}
return 0;
}
void fun(char *s,char *s1)
{
int count=0,i=0,j=0;
for (i= 0; i <strlen(s); ++i)
{
if(i==0)
{
count=1;
}
else if(s[i]==s[i1])
{
count++;
}
else
{
s1[j++]=count+'0';
s1[j++]=s[i1];
count=1;
}
}
s1[j++]=count+'0';
s1[j++]=s[i1];
s1[j]='\0';
printf("%s\n",s1);
}

mani 4m sklm
June 26, 2013 we can eliminate A and B if both knows or both doesnot know each other
 mani 4m sklm June 18, 2013can't we sort it and do?sort  O(nlogn) and then check from i=0 to n a[i] and a[i+k] if both equal then a[i] is the number.
 mani 4m sklm March 11, 2013we can try like this.Please comment me if I am wrong,
first check number of ve numbers(1 pass).say x.take two indices one from 0 another from x.check a[0] and a[x].
case 0: + and  > swap them and move both indices one step ahead
case 1:  and  >move the first index till a + is found and swap them and then move both one step ahead
case 2: + and  viceversa of case 2

mani 4m sklm
March 06, 2013 It is a np complete subset problem and hence can be done by backtracking
 mani 4m sklm March 04, 2013#include <stdio.h>
void merge(int a[],int low,int high);
void mergesort(int a[],int low,int high);
int binarysearch(int a[],int i,int j,int key);
int main(int argc, char const *argv[])
{
int *a,i=0,key,left,right;
a=(int *)malloc(sizeof(int)*10);
printf("data\n");
a[i]=rand()%10;
for ( i = 1; i < 10; ++i)
{
a[i]=a[i1]+(rand()%20);
}
for ( i = 0; i < 10; ++i)
{
printf("%d ",a[i] );
}
printf("\n");
printf("key\n");
scanf("%d",&key);
mergesort(a,0,9);
i=binarysearch(a,0,9,key/2);
if(i<=0i==9)
{
printf("no elements\n");
return 0;
}
left=i1;
right=i;
while(left>=0&&right<=9)
{
if(a[left]+a[right]>key)
{
left;
}
else if(a[left]+a[right]<key)
{
right++;
}
else
{
printf("%d %d\n",a[left],a[right] );
if(left<=0 && right>=9) break;
if(left>0) left;
else if(right<9) right++;
}
}
return 0;
}
int binarysearch(int a[],int i,int j,int key)
{
if(i>j) return j;
int mid=(i+j)/2;
if(a[mid]==key)
{
return mid;
}
else if(mid+1<=j && a[mid]<key && a[mid+1]>key)
{
return mid;
}
else if(a[mid]<key)
{
return binarysearch(a,mid+1,j,key);
}
else
{
if(mid1>=0 && a[mid1]<key && a[mid]>key)
{
return (mid1);
}
else return binarysearch(a,i,mid1,key);
}
}
void mergesort(int a[],int low,int high)
{
int m;
if(low<high)
{
m=(low+high)/2;
mergesort(a,low,m);
mergesort(a,m+1,high);
merge(a,low,high);
}
}
void merge(int a[],int low,int high)
{
if(low>=high) return;
int b[highlow+1],mid=(low+high)/2;
int i=low,j=mid+1,k=1;
for(;i<=mid && j<=high;)
{
while(a[i]<a[j] && i<=mid && j<=high)
{
b[++k]=a[i];
i++;
}
while(a[i]>=a[j] && i<=mid && j<=high)
{
b[++k]=a[j];
j++;
}
}
for(;i<=mid;i++)
b[++k]=a[i];
for(;j<=high;j++)
b[++k]=a[j];
for (k=0, i = low; i <=high; ++i,++k)
{
a[i]=b[k];
}
}

mani 4m sklm
March 03, 2013 try this.sort the array first and then go from last .check whether last element is smaller than number.If yes then get the remainder.Have a binary search of it and then take the largest of all numbers which are smaller than the remainder.continue this.Please correct me if I am wrong.
 mani 4m sklm March 03, 2013let us do it recursively.please correct me if I am wrong
a[0...n1] now add a[n].then let A(n1) be the number of valid interpretations.then A(n)=A(n1)+A(n2) if a[n1]<3 else A(n1)+1.Hence we can do it by dynamic programming
here u can't free it as str is previously allocated memory
in stack.only heap memory can be freed;
count=0;
for(i=0;i<5;i++)
{
for(x=0;x<=count;x++)
{
printf("%d",a[x][countx]);
}
printf("\n");
count++;
}
 mani 4m sklm March 03, 2013correct me if i am wrong
 mani 4m sklm February 09, 2013#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s
{
char a[10];
char b[10];
struct s *next;
}node;
node *ptr[26]={NULL};
int issame(char p[],char q[]);
int main(int argc, char const *argv[])
{
FILE *fp;
int i;
node *temp2;
char temp[10];
int response;
char temp1[11];
fp=fopen("phonesim.txt","r");
if(fp==NULL)
{
printf("no such file found\n");
exit(1);
}
while(fscanf(fp,"%s %s\n",temp,temp1)!=EOF)
{
temp2=ptr[temp[0]'a'];
ptr[temp[0]'a']=malloc(sizeof(node));
strcpy(ptr[temp[0]'a']>a,temp);
temp1[10]='\0';
strcpy(ptr[temp[0]'a']>b,temp1);
ptr[temp[0]'a']>next=temp2;
}
fclose(fp);
printf("enter response\n");
scanf("%d",&response);
while(response)
{
printf("enter string\n");
getchar();
gets(temp);
if(temp[0]>'z'temp[0]<'a')
{
printf("wrong i.p\n");
}
else
{
temp2=ptr[temp[0]'a'];
while(temp2!=NULL)
{
if(issame(temp2>a,temp))
printf("%s %s\n",temp2>a,temp2>b);
temp2=temp2>next;
}
}
printf("enter response\n");
scanf("%d",&response);
}
return 0;
}
int issame(char p[],char q[])
{
int j=0;
int i=strlen(q);
while(j<i)
{
if(p[j]!=q[j])
{
return 0;
}
j++;
}
return 1;
}

mani 4m sklm
February 09, 2013 Assume given numbers are sorted.Then create a temp array obtained by substracting elements from the array but in reverse fashion.Then check the 2 sorted arrays.Eg:10 12 15 18 20 and sum is 33 then temp array is 13 15 18 21 23.Then check 10 and 13.10<13 then move to 12.12<13.then 14<13(no)so 14 and 15.so on O(n)
 mani 4m sklm February 08, 2013Use 26 buckets
 mani 4m sklm February 08, 2013n/2 + 1 means 5/2+1=3.5?
 mani 4m sklm February 06, 20132^(n1)
 mani 4m sklm February 06, 2013A small extension for this que.I was asked to cut in 8 pieces such that everybody gets cream also.Note that in the above que 4 people wont get a cream
 mani 4m sklm February 06, 2013khasif is right.
 mani 4m sklm February 06, 2013i think alex is right
 mani 4m sklm February 06, 2013view the cake as cylindrical.cut it along middle completing a circle.This makes 2 cylinders.Now 2 cuts both make + symbol.so 8 pieces
 mani 4m sklm February 05, 2013can u give an example?
 mani 4m sklm February 05, 2013Open Chat in New Window
I think the given input is tree and not for sure a binary tree. Hence, we have to check number of children for any node is not greater than 2
 mani 4m sklm May 03, 2014