Count of pairs (x, y) in an array such that x < y
Given an array of N distinct integers, the task is to find the number of pairs (x, y) such that x < y.
Example:
Input: arr[] = {2, 4, 3, 1}
Output: 6
Possible pairs are (1, 2), (1, 3), (1, 4), (2, 3), (2, 4) and (3, 4).
Input: arr[] = {5, 10}
Output: 1
The only possible pair is (5, 10).
Naive approach: Find every possible pair and check whether it satisfies the given condition or not.
C++
#include <iostream>
using namespace std;
int getPairs( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (a[i] < a[j])
count++;
}
}
return count;
}
int main()
{
int a[] = { 2, 4, 3, 1 };
int n = sizeof (a) / sizeof (a[0]);
cout << getPairs(a, n);
return 0;
}
|
Java
class GFG {
static int getPairs( int a[])
{
int count = 0 ;
for ( int i = 0 ; i < a.length; i++) {
for ( int j = 0 ; j < a.length; j++) {
if (a[i] < a[j])
count++;
}
}
return count;
}
public static void main(String[] args)
{
int a[] = { 2 , 4 , 3 , 1 };
System.out.println(getPairs(a));
}
}
|
Python 3
def getPairs(a):
count = 0
for i in range ( len (a)):
for j in range ( len (a)):
if (a[i] < a[j]):
count + = 1
return count
if __name__ = = "__main__" :
a = [ 2 , 4 , 3 , 1 ]
print (getPairs(a))
|
C#
using System;
class GFG
{
static int getPairs( int []a)
{
int count = 0;
for ( int i = 0; i < a.Length; i++)
{
for ( int j = 0; j < a.Length; j++)
{
if (a[i] < a[j])
count++;
}
}
return count;
}
public static void Main()
{
int []a = { 2, 4, 3, 1 };
Console.WriteLine(getPairs(a));
}
}
|
PHP
<?php
function getPairs( $a )
{
$count = 0;
for ( $i = 0; $i < sizeof( $a ); $i ++)
{
for ( $j = 0; $j < sizeof( $a ); $j ++)
{
if ( $a [ $i ] < $a [ $j ])
$count ++;
}
}
return $count ;
}
$a = array (2, 4, 3, 1);
echo getPairs( $a );
?>
|
Javascript
<script>
function getPairs(a)
{
let count = 0;
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a.length; j++) {
if (a[i] < a[j])
count++;
}
}
return count;
}
let a=[ 2, 4, 3, 1 ];
document.write(getPairs(a));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Efficient approach: For an element x. In order to find the count of valid pairs of the form (x, y1), (x, y2), …, (x, yn), we need to count the elements which are greater than x. For the smallest element, there will be n – 1 element greater than it. Similarly, the second smallest element can form n – 2 pairs and so on. Therefore, the desired count of valid pairs will be (n – 1) + (n – 2) + …. + 1 = n * (n – 1) / 2 where n is the length of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getPairs( int a[])
{
int n = sizeof (a[0]);
int count = (n * (n - 1)) / 2;
return count;
}
int main()
{
int a[] = { 2, 4, 3, 1 };
cout << getPairs(a);
return 0;
}
|
Java
class GFG {
static int getPairs( int a[])
{
int n = a.length;
int count = (n * (n - 1 )) / 2 ;
return count;
}
public static void main(String[] args)
{
int a[] = { 2 , 4 , 3 , 1 };
System.out.println(getPairs(a));
}
}
|
Python
def getPairs(a):
n = len (a)
count = (n * (n - 1 )) / / 2
return count
a = [ 2 , 4 , 3 , 1 ]
print (getPairs(a))
|
C#
using System;
class GFG
{
static int getPairs( int []a)
{
int n = a.Length;
int count = (n * (n - 1)) / 2;
return count;
}
public static void Main()
{
int []a = { 2, 4, 3, 1 };
Console.Write(getPairs(a));
}
}
|
Javascript
<script>
function getPairs(a)
{
let n = a.length;
let count = parseInt((n * (n - 1)) / 2, 10);
return count;
}
let a = [ 2, 4, 3, 1 ];
document.write(getPairs(a));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...