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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find pairs (i, j) such that // |arr[i]| and |arr[j]| lies in between // |arr[i] - arr[j]| and |arr[i] + arr[j]| void findPairs( int arr[], int N)
{ // Calculate absolute value
// of all array elements
for ( int i = 0; i < N; i++)
arr[i] = abs (arr[i]);
// Sort the array
sort(arr, arr + N);
int left = 0;
// Stores the count of pairs
int ans = 0;
// Traverse the array
for ( int right = 0; right < N; right++) {
while (2 * arr[left] < arr[right])
// Increment left
left++;
// Add to the current
// count of pairs
ans += (right - left);
}
// Print the answer
cout << ans;
} // Driver Code int main()
{ int arr[] = { 1, 3, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
findPairs(arr, N);
return 0;
} |
// Java program for the above approach import java.util.Arrays;
class GFG{
// Function to find pairs (i, j) such that // |arr[i]| and |arr[j]| lies in between // |arr[i] - arr[j]| and |arr[i] + arr[j]| static void findPairs( int arr[], int N)
{ // Calculate absolute value
// of all array elements
for ( int i = 0 ; i < N; i++)
arr[i] = Math.abs(arr[i]);
// Sort the array
Arrays.sort(arr);
int left = 0 ;
// Stores the count of pairs
int ans = 0 ;
// Traverse the array
for ( int right = 0 ; right < N; right++)
{
while ( 2 * arr[left] < arr[right])
// Increment left
left++;
// Add to the current
// count of pairs
ans += (right - left);
}
// Print the answer
System.out.print(ans);
} // Driver Code public static void main(String[] args)
{ int arr[] = { 1 , 3 , 5 , 7 };
int N = arr.length;
findPairs(arr, N);
} } // This code is contributed by AnkThon |
# Python3 program for the above approach # Function to find pairs (i, j) such that # |arr[i]| and |arr[j]| lies in between # |arr[i] - arr[j]| and |arr[i] + arr[j]| def findPairs(arr, N):
# Calculate absolute value
# of all array elements
for i in range (N):
arr[i] = abs (arr[i])
# Sort the array
arr.sort()
left = 0
# Stores the count of pairs
ans = 0
# Traverse the array
for right in range (N):
while ( 2 * arr[left] < arr[right]):
# Increment left
left + = 1
# Add to the current
# count of pairs
ans + = (right - left)
# Print the answer
print (ans)
# Driver Code if __name__ = = "__main__" :
arr = [ 1 , 3 , 5 , 7 ]
N = len (arr)
findPairs(arr, N)
# This code is contributed by ukasp.
|
// C# program for the above approach using System;
class GFG{
// Function to find pairs (i, j) such that // |arr[i]| and |arr[j]| lies in between // |arr[i] - arr[j]| and |arr[i] + arr[j]| static void findPairs( int []arr, int N)
{ // Calculate absolute value
// of all array elements
for ( int i = 0; i < N; i++)
arr[i] = Math.Abs(arr[i]);
// Sort the array
Array.Sort(arr);
int left = 0;
// Stores the count of pairs
int ans = 0;
// Traverse the array
for ( int right = 0; right < N; right++)
{
while (2 * arr[left] < arr[right])
// Increment left
left++;
// Add to the current
// count of pairs
ans += (right - left);
}
// Print the answer
Console.Write(ans);
} // Driver Code public static void Main( string [] args)
{ int []arr = { 1, 3, 5, 7 };
int N = arr.Length;
findPairs(arr, N);
} } // This code is contributed by AnkThon |
<script> // JavaScript program for the above approach // Function to find pairs (i, j) such that // |arr[i]| and |arr[j]| lies in between // |arr[i] - arr[j]| and |arr[i] + arr[j]| function findPairs(arr, N)
{ // Calculate absolute value
// of all array elements
for (let i = 0; i < N; i++)
arr[i] = Math.abs(arr[i]);
// Sort the array
arr.sort((a, b) => a - b);
let left = 0;
// Stores the count of pairs
let ans = 0;
// Traverse the array
for (let right = 0; right < N; right++) {
while (2 * arr[left] < arr[right])
// Increment left
left++;
// Add to the current
// count of pairs
ans += (right - left);
}
// Print the answer
document.write(ans);
} // Driver Code let arr = [ 1, 3, 5, 7 ];
let N = arr.length;
findPairs(arr, N);
// This code is contributed by Manoj. </script> |
2
Time Complexity: O(N*log N)
Auxiliary Space: O(1)