Count of unordered pairs (x, y) of Array which satisfy given equation

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: 2
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: 1
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
  
// Driven code
int main()
{
    int a[] = { 3, 6 };
    int n = sizeof(a)
            / sizeof(a[0]);
  
    cout << numPairs(a, n)
         << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
  
// Driven 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
  
// Driven 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

chevron_right


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

Check out this Author's contributed articles.

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.



Improved By : rock_cool, Code_Mech