Skip to content
Related Articles

Related Articles

Count of unordered pairs (x, y) of Array which satisfy given equation
  • Difficulty Level : Medium
  • Last Updated : 26 Mar, 2021

Given an array arr[] of N integers both positive and negative, our task is to find the number of unordered pairs (x, y) which satisfy the given equations: 

  • | x | > = min(| x – y|, | x + y|)
  • | y | < = max(| x – y|, | x + y|)

Examples: 

Input: arr[] = [ 2, 5, -3 ] 
Output:
Explanation: 
Possible unordered pairs are (5, -3) and (2, -3). (2, 5) is not counted because it does not satisfy the conditions. 

Input: arr[] = [ 3, 6 ] 
Output:
Explanation: 
The pair [3, 6] satisfies the condition. Hence, the output is 1. 

Naive Approach: 
To solve the problem mentioned above the naive method is to traverse through all possible pairs and count no of unordered pairs that satisfy these conditions. But this method is costly and takes more time which can be optimized further.



Time Complexity: O(N2
Auxiliary space: O(1)

Efficient Approach: 
To optimize the above method the main idea is to sort the array and then apply binary search to find out the right boundary for each index in the array. We have to observe that if we change x into – x, then the values of |x| and |y| stay the same, while |x – y| and |x + y| will swap values. This means that the pair {x, y} works if and only if {-x, y} works. Similarly, we can switch the sign of y. This means that we can replace x and y by their absolute values, and the original pair works if and only if the new one works.

If x > = 0 and y > = 0 then the condition becomes |x – y | < = x, y < = x + y. The upper bound always holds, while the lower bound is equivalent to x < = 2 * y and y < = 2 * x

So the problem reduces to counting the number of pairs {x, y} with |x| <= 2|y| and |y| <= 2|x|. To solve this we take the absolute values of all the A[i] and sort the array that is the number of pairs (l, r) with l < r and a[r] < = 2 * a[l]. For each fixed l we calculate the largest r that satisfies this condition, and just add it with the answer.

Below is the implementation of above approach: 

C++




// C++ Program to find the number of
// unordered pairs (x, y) which satisfy
// the given equation for the array
 
#include <bits/stdc++.h>
using namespace std;
 
// Return the number of unordered
// pairs satisfying the conditions
int numPairs(int a[], int n)
 
{
    int ans, i, index;
 
    // ans stores the number
    // of unordered pairs
    ans = 0;
 
    // Making each value of
    // array to positive
    for (i = 0; i < n; i++)
        a[i] = abs(a[i]);
 
    // Sort the array
    sort(a, a + n);
 
    // For each index calculating
    // the right boundary for
    // the unordered pairs
    for (i = 0; i < n; i++) {
 
        index = upper_bound(
                    a,
                    a + n,
                    2 * a[i])
                - a;
        ans += index - i - 1;
    }
 
    // Return the final result
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 3, 6 };
    int n = sizeof(a)
            / sizeof(a[0]);
 
    cout << numPairs(a, n)
         << endl;
 
    return 0;
}

Java




// Java Program to find the number of
// unordered pairs (x, y) which satisfy
// the given equation for the array
import java.util.Arrays;
class GFG{
     
// Return the number of unordered
// pairs satisfying the conditions
static int numPairs(int a[], int n)
{
    int ans, i, index;
 
    // ans stores the number
    // of unordered pairs
    ans = 0;
 
    // Making each value of
    // array to positive
    for (i = 0; i < n; i++)
        a[i] = Math.abs(a[i]);
 
    // Sort the array
    Arrays.sort(a);
 
    // For each index calculating
    // the right boundary for
    // the unordered pairs
    for (i = 0; i < n; i++)
    {
        index = 2;
        ans += index - i - 1;
    }
 
    // Return the final result
    return ans;
}
 
// Driver code
public static void main(String []args)
{
    int a[] = new int[]{ 3, 6 };
    int n = a.length;
 
    System.out.println(numPairs(a, n));
}
}
 
// This code is contributed by rock_cool

Python3




# Python3 program to find the number of
# unordered pairs (x, y) which satisfy
# the given equation for the array
  
# Return the number of unordered
# pairs satisfying the conditions
def numPairs(a, n):
  
    # ans stores the number
    # of unordered pairs
    ans = 0
  
    # Making each value of array
    # to positive
    for i in range(n):
        a[i] = abs(a[i])
  
    # Sort the array
    a.sort()
  
    # For each index calculating
    # the right boundary for
    # the unordered pairs
    for i in range(n):
        index = 0
         
        for j in range(i + 1, n):
            if (2 * a[i] >= a[j - 1] and
                2 * a[i] < a[j]):
                index = j
                 
        if index == 0:
            index = n
             
        ans += index - i - 1
  
    # Return the final result
    return ans
  
# Driver code
a = [ 3, 6 ]
n = len(a)
 
print(numPairs(a, n))
 
# This code is contributed by avanitrachhadiya2155

C#




// C# Program to find the number of
// unordered pairs (x, y) which satisfy
// the given equation for the array
using System;
class GFG{
     
// Return the number of unordered
// pairs satisfying the conditions
static int numPairs(int []a, int n)
{
    int ans, i, index;
 
    // ans stores the number
    // of unordered pairs
    ans = 0;
 
    // Making each value of
    // array to positive
    for (i = 0; i < n; i++)
        a[i] = Math.Abs(a[i]);
 
    // Sort the array
    Array.Sort(a);
 
    // For each index calculating
    // the right boundary for
    // the unordered pairs
    for (i = 0; i < n; i++)
    {
        index = 2;
        ans += index - i - 1;
    }
 
    // Return the final result
    return ans;
}
 
// Driver code
public static void Main()
{
    int []a = new int[]{ 3, 6 };
    int n = a.Length;
 
    Console.Write(numPairs(a, n));
}
}
 
// This code is contributed by Code_Mech

Javascript




<script>
    // Javascript Program to find the number of
    // unordered pairs (x, y) which satisfy
    // the given equation for the array
     
    // Return the number of unordered
    // pairs satisfying the conditions
    function numPairs(a, n)
    {
        let ans, i, index;
 
        // ans stores the number
        // of unordered pairs
        ans = 0;
 
        // Making each value of
        // array to positive
        for (i = 0; i < n; i++)
            a[i] = Math.abs(a[i]);
 
        // Sort the array
        a.sort();
 
        // For each index calculating
        // the right boundary for
        // the unordered pairs
        for (i = 0; i < n; i++)
        {
            index = 2;
            ans += index - i - 1;
        }
 
        // Return the final result
        return ans;
    }
 
    let a = [ 3, 6 ];
    let n = a.length;
  
    document.write(numPairs(a, n));
     
</script>
Output: 
1

 

Time Complexity: O(n * log n) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :