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++
#include <bits/stdc++.h>
using namespace std;
int numPairs( int a[], int n)
{
int ans, i, index;
ans = 0;
for (i = 0; i < n; i++)
a[i] = abs (a[i]);
sort(a, a + n);
for (i = 0; i < n; i++) {
index = upper_bound(
a,
a + n,
2 * a[i])
- a;
ans += index - i - 1;
}
return ans;
}
int main()
{
int a[] = { 3, 6 };
int n = sizeof (a)
/ sizeof (a[0]);
cout << numPairs(a, n)
<< endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static int numPairs( int a[], int n)
{
int ans, i, index;
ans = 0 ;
for (i = 0 ; i < n; i++)
a[i] = Math.abs(a[i]);
Arrays.sort(a);
for (i = 0 ; i < n; i++)
{
index = 2 ;
ans += index - i - 1 ;
}
return ans;
}
public static void main(String []args)
{
int a[] = new int []{ 3 , 6 };
int n = a.length;
System.out.println(numPairs(a, n));
}
}
|
Python3
def numPairs(a, n):
ans = 0
for i in range (n):
a[i] = abs (a[i])
a.sort()
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 ans
a = [ 3 , 6 ]
n = len (a)
print (numPairs(a, n))
|
C#
using System;
class GFG{
static int numPairs( int []a, int n)
{
int ans, i, index;
ans = 0;
for (i = 0; i < n; i++)
a[i] = Math.Abs(a[i]);
Array.Sort(a);
for (i = 0; i < n; i++)
{
index = 2;
ans += index - i - 1;
}
return ans;
}
public static void Main()
{
int []a = new int []{ 3, 6 };
int n = a.Length;
Console.Write(numPairs(a, n));
}
}
|
Javascript
<script>
function numPairs(a, n)
{
let ans, i, index;
ans = 0;
for (i = 0; i < n; i++)
a[i] = Math.abs(a[i]);
a.sort();
for (i = 0; i < n; i++)
{
index = 2;
ans += index - i - 1;
}
return ans;
}
let a = [ 3, 6 ];
let n = a.length;
document.write(numPairs(a, n));
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!