I was trying to solve this problem: Given a number N and two arrays of A, B of N number of elements determine if B contains the same elements as A (not necessarily at the same position).
There will be three inputs:
First line contains a number N number of elements
Second line contains N numbers elements of array A
Third line contains N numbers elements of array B
Examples:
input:
4
4 2 3 7
2 3 4 9
output: no
input:
5
5 1 1 9 3
1 9 1 5 3
output: yes
My Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
long int a[n], b[n];
for (int i = 0; i < n; i++)
{
scanf("%ld", &a[i]);
}
for (int i = 0; i < n; i++)
{
scanf("%ld", &b[i]);
}
int same;
for (int j = 0; j < n; j++)
{
same = 0;
for (int k = 0; k < n; k++)
{
if (a[j] == b[k])
{
same = 1;
}
}
if (same == 0)
{
break;
}
}
if (same == 0)
{
printf("no\n");
}
else
{
printf("yes\n");
}
return 0;
}
My code will take the first element of the array A and compare it with all the elements of array B and then take the following elements of A and do the same. This approach could successfully give correct output for the first 3 test cases (only 2 were shown in the question) but it shows wrong answer on test 4 and I don't know why as the input of that test case is not shown. So, please help me find the problem in my code.
Your approach, which has quadratic time complexity, does not work if the arrays have the same numbers but in differing amounts: eg:
1 1 2
and1 2 2
will produceyes
eventhough the arrays do not strictly contain the same numbers in a possibly different order.You could avoid this problem using an extra array of booleans storing whether
b[k]
has already been used.A more efficient approach is to make copies of both arrays, sort them and compare the sorted arrays (complexity O(N.log(N))).
Here is a modified version using the extra array of booleans:
Here is a more efficient version (for large sets), that sorts the arrays using
qsort
:Finally, here is an alternative using a hash table that should have quasi linear time complexity at the cost of extra memory:
In practice, the
hash
version is only slightly faster than theqsort
version for large random sets (100k or 1 million random values) because the overhead of hashing is large, the hash function is simplistic and the log factor in sorting is small.Here is a benchmark for 100000 random numbers (and a shuffled array):