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
Only possible pair is (5, 10)
Naive approach: Find every possible pair and check whether it satisfies the given condition or not.
Java
// Java implementation of the approach class GFG { // Function to return the number of // pairs (x, y) such that x < y static int getPairs(int a[]) { // To store the number of valid pairs int count = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { // If a valid pair is found if (a[i] < a[j]) count++; } } // Return the count of valid pairs return count; } // Driver code public static void main(String[] args) { int a[] = { 2, 4, 3, 1 }; System.out.println(getPairs(a)); } } |
Python 3
# Python3 implementation of the approach # Function to return the number of # pairs (x, y) such that x < y def getPairs(a): # To store the number of valid pairs count = 0 for i in range(len(a)): for j in range(len(a)): # If a valid pair is found if (a[i] < a[j]): count += 1 # Return the count of valid pairs return count # Driver code if __name__ == "__main__": a = [ 2, 4, 3, 1 ] print(getPairs(a)) # This code is contributed by ita_c |
C#
// C# implementation of the approach using System; class GFG { // Function to return the number of // pairs (x, y) such that x < y static int getPairs(int []a) { // To store the number of valid pairs int count = 0; for (int i = 0; i < a.Length; i++) { for (int j = 0; j < a.Length; j++) { // If a valid pair is found if (a[i] < a[j]) count++; } } // Return the count of valid pairs return count; } // Driver code public static void Main() { int []a = { 2, 4, 3, 1 }; Console.WriteLine(getPairs(a)); } } // This code is contributed by Ryuga |
PHP
<?php // PHP implementation of the approach // Function to return the number of // pairs (x, y) such that x < y function getPairs($a) { // To store the number of valid pairs $count = 0; for ($i = 0; $i < sizeof($a); $i++) { for ($j = 0; $j < sizeof($a); $j++) { // If a valid pair is found if ($a[$i] < $a[$j]) $count++; } } // Return the count of valid pairs return $count; } // Driver code $a = array(2, 4, 3, 1); echo getPairs($a); // This code is contributed // by Akanksha Rai ?> |
6
Time Complexity: O(n2)
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 elements 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:
Java
// Java implementation of the approach class GFG { // Function to return the number of // pairs (x, y) such that x < y static int getPairs(int a[]) { // Length of the array int n = a.length; // Calculate the number of valid pairs int count = (n * (n - 1)) / 2; // Return the count of valid pairs return count; } // Driver code public static void main(String[] args) { int a[] = { 2, 4, 3, 1 }; System.out.println(getPairs(a)); } } |
C#
// C# implementation of the approach using System; class GFG { // Function to return the number of // pairs (x, y) such that x < y static int getPairs(int []a) { // Length of the array int n = a.Length; // Calculate the number of valid pairs int count = (n * (n - 1)) / 2; // Return the count of valid pairs return count; } // Driver code public static void Main() { int []a = { 2, 4, 3, 1 }; Console.Write(getPairs(a)); } } // This code is contributed // by Akanksha Rai |
6
Time Complexity: O(1)
Recommended Posts:
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count pairs in an array that hold i*arr[i] > j*arr[j]
- Count pairs in array whose sum is divisible by K
- Count pairs in a sorted array whose sum is less than x
- Count pairs in an array such that frequency of one is at least value of other
- Count pairs in an array that hold i+j= arr[i]+arr[j]
- Count pairs in array whose sum is divisible by 4
- Count divisible pairs in an array
- Count pairs in an array such that at least one element is prime
- Count pairs in an array which have at least one digit common
- Count pairs with average present in the same array
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count number of pairs in array having sum divisible by K | SET 2
- Count pairs in a sorted array whose product is less than k
- Count of pairs in an array whose sum is a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



