Wednesday, December 30, 2009

Tricky question.....

Que: You have 2 arrays each containing a set of numbers. Write an
algorithm that gets the elements present in the first array and missing
from the second. It should have n complexity.


void fun(int a[], int b[], int N){
int max = a[0];
for(int i =0; i < N;i++)
if(max < a[i])
max = a[i];

int c[max]={0};
for(int i =0; i < N;i++)
c[a[i]]++;

for(int i =0; i < N;i++)
c[b[i]]--;

for(int i =0; i < N;i++)
if(c[a[i]]==1)
print a[i];
}

No comments:

Post a Comment

Search Ranjeet's Blog