Count pair sum (GFG)

 Given two sorted arrays of size m and n of distinct elements. Given a value x. The problem is to count all pairs from both arrays whose sum is equal to x.

Note: The pair has an element from each array.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two integers m and n denoting the size of the two arrays. The next two lines contains the two arrays arr1 and arr2 respectively. The last line contains the value of sum x.

Output:
Print the count of all pairs from both arrays whose sum is equal to x.

Constraints:
1<=T<=100
1<=m,n<=10^5
1<=arr1[i],arr2[j]<=10^5
1<=k<=10^5

Example:
Input:

2
4 4
1 3 5 7
2 3 5 8
10
7 7
1 2 3 4 5 7 11
2 3 4 5 6 8 12
9

Ouput:
2
5


SOLUTION:

T = int(input())

for i in range(0,T):

    ans =0

    m,n = list(map(int,input().split()))

    a = list(map(int,input().split()))

    b = list(map(int,input().split()))

    x = int(input())

    for j in range(0,len(a)):

        for k in range(0,len(b)):

            if a[j]+b[k] == x:

                ans =ans +1

    print(ans)   

Comments

Popular posts from this blog

Sort in specific order (GFG)

Chef and Remissness Problem Code: REMISS (CodeChef)

Short Notes Of computer Network