Counting maximum moves

92 Views Asked by At

Given two arrays, each of size N denoted by A1,A2...AN and B1,B2...BN.

Let us maintain two sets S1 and S2 which are empty initially.

In one move ,Pick a pair of indexes (i, j) such that :

-It's already not present in S1.
-B[j] > A[i]
-GCD(A[i],B[j]) is not 1.

Further, Pick another pair of indexes (p, q) such that :

-It's already not present in S2. 
-B[p] < A[q]
-GCD(A[q],B[p]) is not 1. 
-GCD(A[q],B[p]) should not be co prime to GCD(A[i],B[j]). 

Then add both pair of indexes to S1 and S2, respectively.

I need to find the largest number of moves that can be performed.

Example : Let N=2 and 2 arrays A,B are : [2 5 6 14] , [3 4 7 10]

Then here answer is 3

Explanation : Following are the possible moves denoting by (i,j) and (p,q)

1st move: (1,2) and (2,3)

2nd move: (1,4) and (2,4)

3rd move: (2,4) and (4,4)

In any possible combination not more than 3 moves are possible.

Now provided N ,(which can be upto 500) and 2 arrays A and B I ned to find maximum moves