Good Pairs | Practice | GeeksforGeeks

Given an array of positive integers of size N. Count the number of good pairs (i,j) such that:
1≤ i ≤ N,
1≤ j ≤ N
and arri < arrj.
Example 1
Input:
N = 2
arr[] = {2, 1}  
Output: 1
Explanation : The only good pair is (2,1). 

Example 2
Input:
N = 3
arr[] = {2 ,3, 2}
Output: 2
Explanation: The two good pairs are (2,3) 
and (3,2).

Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the array arr[] and its size N as inputs and returns the count of good pairs as described in the problem description.
Expected Time Complexity: O(N log N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^5
1 <= a[i] <= 10^3
0
sunilece19140226 months ago
long long solve(int arr[], int n)   {      sort(arr,arr+n);      long long ans= ((long long)n*(long long)(n-1))/2;            long long c=1;      for(long long i=0;i<n-1;i++)      {          if(arr[i]==arr[i+1])          {              c++;          }          else          {              ans=ans-(c*(c-1))/2;              c=1;          }      }       ans=ans-(c*(c-1))/2;      return ans;               }
0
Aamir rahman1 year ago
Aamir rahman
#include <iostream>#include<bits stdc++.h="">using namespace std;int main() {//codeint t;cin>>t;while(t--){    int n;cin>>n;    int arr[n];    for(int i=0;i<n;i++) cin="">>arr[i];  sort(arr,arr+n);//   2  2 3// 4 4 4  4 6    int count=0;    int j=n-1;    for(int i=1;i<n;i++){ if(arr[i-1]<arr[i])="" count+="i;" if(arr[i-1]="=arr[i])" {="" int="" j="i-1;" while(j="">=0)        {            if(arr[j]<arr[i]) break;="" j--;="" }="" count+="j+1;" }="" }="" cout<<="" count<<endl;="" }="" return="" 0;="" }<="" code="">
0
morta saidani1 year ago
morta saidani
#include <stdio.h>
void remplir(int *arr, int n) {    for(int i =0; i < n; i++) {        scanf("%d", &arr[i]);    }}
void affiche(int *arr, int n) {    int nb = 0;    for(int i = 0; i < n; i++) {        for(int j = 0; j < n; j++) {            if(arr[i] < arr[j]) {                nb++;            }        }    }    printf("%d\n", nb);}
int main() {//codeint k;scanf("%d", &k);while(k > 0) {    int n;    scanf("%d", &n);    int arr[n];    remplir(arr, n);    affiche(arr, n);    k--;}return 0;}
0
Sumit Pardhiya1 year ago
Sumit Pardhiya
#include <bits stdc++.h="">using namespace std;
int main() {//codeint t;cin>>t;while(t--){    int n,c=0;    cin>>n;    int a[n];    for(int i=0;i<n;i++) {="" cin="">>a[i];    }    sort(a,a+n);    for(int i=0;i<n;i++) {="" for(int="" j="n-1;j">=i+1;j--)       {           if(a[i]<a[j]) {="" c++;="" }="" }="" }="" cout<<c<<endl;="" }="" return="" 0;="" }="">
0
Vikranth1 year ago
Vikranth
can someone tell me why doesn't this logic workhttps://ide.geeksforgeeks.o...
0
Drigger1 year ago
Drigger
Sol in Java, ET: 0.30 : https://ide.geeksforgeeks.o...
0
pradeep gupta1 year ago
pradeep gupta
count=0;    n=sc.nextInt();    int[] arr =new int[n];
    for(int k=0;k<n;k++){ arr[k]="sc.nextInt();" }="" for(int="" i="0;i&lt;n-1;i++){" for(int="" j="i+1;j&lt;n;j++){" if(math.abs(arr[i]-arr[j])="">0){                count++;            }        }    }    System.out.println(count);
-1
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.03
#include<iostream>using namespace std;int goodPair(int arr[], int n){    int count = 0;        for(int i = 0; i < n; i++)    {        for(int j = 0; j < n; j++)        {            if(arr[i] < arr[j])            {                count++;            }        }    }        return count;}int main(){    int T;    cin >> T;        while(T--)    {        int N;        cin >> N;                int arr[N];        for(int i = 0; i < N; i++)        {            cin >> arr[i];        }                cout << goodPair(arr, N) << endl;    }return 0;}
0
erharshit2 years ago
erharshit
#py3#codefrom itertools import combinationst = int(input())while t>0:    n = int(input())    l = list(map(int, input().split()))    c = combinations(l,2)    count = 0    for i in c:        if i[0]!=i[1]:            count+=1    print(count)    t-=1
0
Asif Ali2 years ago
Asif Ali
Correct Answer: 4 line codeExecution Time:0.03
int cnt=0;for (int i=0;i<n;i++) for="" (int="" j="0;j&lt;n;j++)" if="" (a[i]<a[j])="" cnt++;="" cout<<cnt<<endl;="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.