Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Thursday, June 8, 2017

Good Pairs Geeks Solution


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


import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int n=ab.nextInt();
   int count=0;
   int arr[]=new int[n];
   for(int i=0;i<n;i++)
   arr[i]=ab.nextInt();
   Arrays.sort(arr);
   for(int i=0;i<n;i++)
   {
       for(int j=i+1;j<n;j++)
       {
           if(arr[j]>arr[i])
           {
               count+=n-j;
               break;
           }
       }
   }
   System.out.println(count);
}
}
}

0 Comments:

Post a Comment

Stats