Count pairs (i, j) from an array such that |arr[i]| and |arr[j]| both lies between |arr[i] – arr[j]| and |arr[i] + arr[j]|
Last Updated :
20 Feb, 2023
Given an array arr[] of size N, the task is to count the number of pairs (arr[i], arr[j]) such that |arr[i]| and |arr[j]| lies between |arr[i] – arr[j]| and |arr[i] + arr[j]|.
Examples:
Input: arr[] = {1, 3, 5, 7}
Output: 2
Explanation:
Pair (arr[1], arr[2]) (= (3, 5)) lies between |3 – 5| (= 2) and |3 + 5| (= 8).
Pair (arr[2], arr[3]) (= (5, 7)) lies between |5 – 7| (= 2) and |5 + 7| (= 12).
Input: arr[] = {-4, 1, 9, 7, -1, 2, 8}
Output: 9
Approach: The given problem can be solved by analyzing the following cases:
- If X is positive and Y is positive:
- |X – Y| remains |X – Y|.
- |X + Y| remains |X + Y|.
- If X is negative and Y is positive:
- |X – Y| becomes |-(X + Y)|, i.e. |X + Y|.
- |X + Y| becomes |-(X – Y)|, i.e. |X – Y|.
- If X is positive and Y is negative:
- |X – Y| becomes |X + Y|.
- |X + Y| becomes |X – Y|.
- If X is negative and Y is negative:
- |X – Y| remains |X – Y|.
- |X + Y| remains |X + Y|.
It is clear from the above cases, that |X – Y| and |X + Y| are at most swapping values, which does not change the solution.
Therefore, if a pair is valid for (X, Y), then it will also be valid for any of the above cases like (-X, Y). Therefore, the task reduces to just take absolute values of X and Y while finding the solution, i.e. to find (X, Y), where |X – Y| ≤ X, Y ≤ X + Y.
Follow the steps below to solve the problem:
- Take absolute values of all elements present in the array arr[].
- Sort the array arr[].
- Initialize a variable, say left, as 0.
- Initialize a variable, say ans, to store the count of valid pairs.
- Traverse the array arr[] using a variable, say right, and perform the following steps:
- Increment left until 2 *arr[left] is less than arr[right].
- Add the value (i – left) to ans to include the number of valid pairs.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPairs( int arr[], int N)
{
for ( int i = 0; i < N; i++)
arr[i] = abs (arr[i]);
sort(arr, arr + N);
int left = 0;
int ans = 0;
for ( int right = 0; right < N; right++) {
while (2 * arr[left] < arr[right])
left++;
ans += (right - left);
}
cout << ans;
}
int main()
{
int arr[] = { 1, 3, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
findPairs(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static void findPairs( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
arr[i] = Math.abs(arr[i]);
Arrays.sort(arr);
int left = 0 ;
int ans = 0 ;
for ( int right = 0 ; right < N; right++)
{
while ( 2 * arr[left] < arr[right])
left++;
ans += (right - left);
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 5 , 7 };
int N = arr.length;
findPairs(arr, N);
}
}
|
Python3
def findPairs(arr, N):
for i in range (N):
arr[i] = abs (arr[i])
arr.sort()
left = 0
ans = 0
for right in range (N):
while ( 2 * arr[left] < arr[right]):
left + = 1
ans + = (right - left)
print (ans)
if __name__ = = "__main__" :
arr = [ 1 , 3 , 5 , 7 ]
N = len (arr)
findPairs(arr, N)
|
C#
using System;
class GFG{
static void findPairs( int []arr, int N)
{
for ( int i = 0; i < N; i++)
arr[i] = Math.Abs(arr[i]);
Array.Sort(arr);
int left = 0;
int ans = 0;
for ( int right = 0; right < N; right++)
{
while (2 * arr[left] < arr[right])
left++;
ans += (right - left);
}
Console.Write(ans);
}
public static void Main( string [] args)
{
int []arr = { 1, 3, 5, 7 };
int N = arr.Length;
findPairs(arr, N);
}
}
|
Javascript
<script>
function findPairs(arr, N)
{
for (let i = 0; i < N; i++)
arr[i] = Math.abs(arr[i]);
arr.sort((a, b) => a - b);
let left = 0;
let ans = 0;
for (let right = 0; right < N; right++) {
while (2 * arr[left] < arr[right])
left++;
ans += (right - left);
}
document.write(ans);
}
let arr = [ 1, 3, 5, 7 ];
let N = arr.length;
findPairs(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...