Sachin
BAN USERI was asked the same question in Amazon interview. And it was explicitly specified that the number of occurence of character x in A should not be more than the number of ocurences of x in B.
That means:
A=zzz
B=abz ---> A does not exist in B...
A=zzz
B=zabczz ---> A exists in B
If space is not an issue.... We can copy the matix into a sorted one-dim array and
after that, use the following code:
#include<stdio.h>
int main()
{
int a[20];
int num,k,i,c;
printf("Enter number of elements\n");
scanf("%d",&num);
printf("Enter elements\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
printf("Enter k\n");
scanf("%d",&k);
c=1;
for(i=1;i<num;i++)
{
if(c==k)
{
printf("kth Smallest with k=%d is %d",k,a[i-1]);
break;
}
if(a[i]!=a[i-1])
c++;
}
scanf("%d",&i);
return 0;
}
#include<stdio.h>
int main()
{
int a[20];
int num,k,i,c;
printf("Enter number of elements\n");
scanf("%d",&num);
printf("Enter elements\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
printf("Enter k\n");
scanf("%d",&k);
c=1;
for(i=1;i<num;i++)
{
if(c==k)
{
printf("kth Smallest with k=%d is %d",k,a[i-1]);
break;
}
if(a[i]!=a[i-1])
c++;
}
scanf("%d",&i);
return 0;
}
if(a.length != b.length)
return false;
for (int i = 0; i < a.Length; i++)
{
a[i]=a[i]+3;
if (a[i] != 0)
prodA = prodA * a[i];
b[i]=b[i]+3;
if (b[i] != 0)
prodB = prodB * b[i];
}
if (prodA == prodB)
{
//For some extreme cases
for (int i = 0; i < a.Length; i++)
{
a[i]=a[i]+5;
if (a[i] != 0)
prodA = prodA * a[i];
b[i]=b[i]+5;
if (b[i] != 0)
prodB = prodB * b[i];
}
}
else
return false;
if(prodA == prodB)
return true;
else
return false;
We can add 1st 2 prime numbers one=by-one to each element of both the arrays.
Compare the products.
if(a.length != b.length)
return false;
for (int i = 0; i < a.Length; i++)
{
a[i]=a[i]+3;
if (a[i] != 0)
prodA = prodA * a[i];
b[i]=b[i]+3;
if (b[i] != 0)
prodB = prodB * b[i];
}
if (prodA == prodB)
{
//For some extreme cases
for (int i = 0; i < a.Length; i++)
{
a[i]=a[i]+5;
if (a[i] != 0)
prodA = prodA * a[i];
b[i]=b[i]+5;
if (b[i] != 0)
prodB = prodB * b[i];
}
}
else
return false;
if(prodA == prodB)
return true;
else
return false;
it should be sb.append(matrix[j][i]) for transpose...
- Sachin January 18, 2014