Achieve Internet Interview Question
AnalystsTeam: artificial intelligence team
Country: United States
Interview Type: Written Test
there is compile error here
..
// Uses counting sort (or bucket sort) - O(n+m)
std::vector<int> count_sort(101);
for (int n : nvec)
count_sort[n]++;
for ( int n : mvec)
count_sort[n]++;
@Marey no, there's not. Please use a recent compiler (at least supporting C++11). You can try the code on ideone.com if you prefer.
#!/usr/bin/python
import fileinput
import array
i = 0;
for line in fileinput.input():
if i == 0:
num_cases = int(line)
elif (i-1) % 3 == 0:
arr = array.array('i',(0,)*100)
[len1, len2] = [int(t) for t in line.split()]
elif (i-2) % 3 == 0:
for t in line.split():
arr[int(t)]+=1
elif (i-3) % 3 == 0:
cnt=0
for t in line.split():
if arr[int(t)]==1:
cnt+=1
print "%d" % cnt
i+=1
#include<stdio.h>
int main()
{
int a[101],b[101], c[101];
int i,j=0,k,n,m;
printf(" enter no. of test cases");
scanf("%d",&k);
while(k--){
printf("enter the sequence");
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
c[a[i]]=1;
}
for(i=0;i<m;i++)
{
if(c[b[i]] == 1)
j++;
}
printf("%d\n",j);
}
return 0;
}
My algorithm uses counting sort (or bucket sort) in O(n+m) and then count how many the elements are duplicated (count = 2). Written in C++.
Input:
Output:
Code:
- Diego Giagio December 10, 2013