Good Pairs (GFG)

 You are given an array of positive integer numbers a1,a2...aN.

Count number of pairs (i,j) such that:
1≤ i ≤ N
1≤ j ≤ N
ai < aj


Input:

The first line contains 'T' denoting the number of test cases. Then follows description of testcases.Each case begins with a single positive integer N denoting the size of array.The second line contains N space separated positive integers denoting the elements of array.
 

Output:

For each test case, output a single line containing number of pairs for corresponding test case.

Constraints:

1<=T<=20
1<=N<=10^3
1<=a[i]<=10^3

Example:

Input :
2
2
2 1  
3
2 3 2
 
Output : 
1
2

Explanation:

In the first test case, the only good pair is (2,1). 
In the second test case, the two good pairs are (2,3) and (3,2).

SOLUTION:

t=int(input())

for i in range (t):

    n=int(input())

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

    pair,lastpair = 0,0

    p.sort()

    for i in range(1,n):

        if p[i]==p[i-1]:

            pair+=lastpair

        else:

            pair+=i

            lastpair = i

    print(pair)

Comments

Popular posts from this blog

Sort in specific order (GFG)

Chef and Remissness Problem Code: REMISS (CodeChef)

Short Notes Of computer Network