Given two unsorted arrays A[i] & B[j], you need to find a "pair of numbers" satisfying the following:
Now, count such pairs in the given two unsorted array of elements. Note, that the two arrays has same number of elements.
Method 1:
- Sort the array A in Descending order
- Sort the array B in Ascending order
- set index i to 1, count = 0 and check whether A[i] % B[i] == 0 OR B[i] % A[i] == 0
- If the above condition satisfied, increment count else continue the loop until N, the number of elements in both of the arrays.
#include <stdio.h>
/* Array size */
#define MAX 1000000
int main()
{
int B[MAX],G[MAX];
int t,N,i,count,j,temp;
/* Get the number of test cases from the user */
scanf("%d",&t);
while(t--)
{
/* Initialize the count as zero for every test case */
count=0;
/* Get the number of elements of the array */
scanf("%d",&N);
/* Get the elements of the array 1 */
for(i=0;i<N;i++)
{
scanf("%d",&B[i]);
}
/* Get the elements of the array 2 */
for(i=0;i<N;i++)
{
scanf("%d",&G[i]);
}
for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
{
if(B[i]<B[j])
continue;
else
{
temp=B[i];
B[i]=B[j];
B[j]=temp;
}
}
for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
{
if(G[i]>G[j])
continue;
else
{
temp=G[i];
G[i]=G[j];
G[j]=temp;
}
}
/* For every pair, increment the count */
for(i=0;i<N;i++)
if((B[i]%G[i])==0 || (G[i]%B[i])==0)
count++;
/* Print the count for every test case */
printf("%d\n",count);
}
return 0;
}