Count pairs from an array with absolute difference not less than the minimum element in the pair
Given an array arr[] consisting of N positive integers, the task is to find the number of pairs (arr[i], arr[j]) such that absolute difference between the two elements is at least equal to the minimum element in the pair.
Examples:
Input: arr[] = {1, 2, 2, 3}
Output: 3
Explanation:
Following are the pairs satisfying the given criteria:
- (arr[0], arr[1]): The absolute difference between the two is abs(1 – 2) = 1, which is at least the minimum of the two i.e., min(1. 2) = 1.
- (arr[0], arr[2]): The absolute difference between the two is abs(1 – 2) = 1, which is at least the minimum of the two i.e., min(1. 2) = 1.
- (arr[0], arr[3]): The absolute difference between the two is abs(1 – 2) = 1, which is at least the minimum of the two i.e., min(1. 2) = 1.
Therefore, the total count of such pairs is 3.
Input: arr[] = {2, 3, 6}
Output: 2
Naive Approach: The simple approach to solve the given problem is to generate all possible pairs from the array and count those pairs that satisfy the given conditions. After checking for all the pairs, print the total count obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by sorting the given array and then iterate two nested loops such that the first loop, iterate till N and the second loop, iterate from arr[i] – (i%arr[i]) with the increment of j as j += arr[i] till N and count those pairs that satisfy the given conditions. Follow the steps below to solve the problem:
- Initialize the variable, say count as 0 that stores the resultant count of pairs.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- Iterate over the range [arr[i] – (i%arr[i]), N] using the variable j with the increment of j as j += arr[i] and if i is less than j and abs(arr[i] – arr[j]) is at least the minimum of arr[i] and arr[j] then increment the count by 1.
- After performing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the number of pairs// (i, j) such that abs(a[i]-a[j]) is// at least the minimum of (a[i], a[j])int getPairsCount(int arr[], int n){ // Stores the resultant count of pairs int count = 0; // Iterate over the range [0, n] for (int i = 0; i < n; i++) { // Iterate from arr[i] - (i%arr[i]) // till n with an increment // of arr[i] for (int j = arr[i] - (i % arr[i]); j < n; j += arr[i]) { // Count the possible pairs if (i < j && abs(arr[i] - arr[j]) >= min(arr[i], arr[j])) { count++; } } } // Return the total count return count;}// Driver Codeint main(){ int arr[] = { 1, 2, 2, 3 }; int N = sizeof(arr) / sizeof(arr[0]); cout << getPairsCount(arr, N); return 0;} |
Java
// Java program for the above approachimport java.io.*;class GFG{ // Function to find the number of pairs // (i, j) such that abs(a[i]-a[j]) is // at least the minimum of (a[i], a[j]) static int getPairsCount(int arr[], int n) { // Stores the resultant count of pairs int count = 0; // Iterate over the range [0, n] for (int i = 0; i < n; i++) { // Iterate from arr[i] - (i%arr[i]) // till n with an increment // of arr[i] for (int j = arr[i] - (i % arr[i]); j < n; j += arr[i]) { // Count the possible pairs if (i < j && Math.abs(arr[i] - arr[j]) >= Math.min(arr[i], arr[j])) { count++; } } } // Return the total count return count; } // Driver Code public static void main(String[] args) { int arr[] = { 1, 2, 2, 3 }; int N = arr.length; System.out.println(getPairsCount(arr, N)); }}// This code is contributed by Potta Lokesh |
Python3
# Python 3 program for the above approach# Function to find the number of pairs# (i, j) such that abs(a[i]-a[j]) is# at least the minimum of (a[i], a[j])def getPairsCount(arr, n): # Stores the resultant count of pairs count = 0 # Iterate over the range [0, n] for i in range(n): # Iterate from arr[i] - (i%arr[i]) # till n with an increment # of arr[i] for j in range(arr[i] - (i % arr[i]),n,arr[i]): # Count the possible pairs if (i < j and abs(arr[i] - arr[j]) >= min(arr[i], arr[j])): count += 1 # Return the total count return count# Driver Codeif __name__ == '__main__': arr = [1, 2, 2, 3] N = len(arr) print(getPairsCount(arr, N)) # This code is contributed by ipg2016107. |
C#
// C# program for above approachusing System;class GFG{ // Function to find the number of pairs // (i, j) such that abs(a[i]-a[j]) is // at least the minimum of (a[i], a[j]) static int getPairsCount(int[] arr, int n) { // Stores the resultant count of pairs int count = 0; // Iterate over the range [0, n] for (int i = 0; i < n; i++) { // Iterate from arr[i] - (i%arr[i]) // till n with an increment // of arr[i] for (int j = arr[i] - (i % arr[i]); j < n; j += arr[i]) { // Count the possible pairs if (i < j && Math.Abs(arr[i] - arr[j]) >= Math.Min(arr[i], arr[j])) { count++; } } } // Return the total count return count; }// Driver Codepublic static void Main(String[] args){ int[] arr = { 1, 2, 2, 3 }; int N = arr.Length; Console.Write(getPairsCount(arr, N)); }}// This code is contributed by sanjoy_62. |
Javascript
<script>// JavaScript program for the above approach// Function to find the number of pairs// (i, j) such that abs(a[i]-a[j]) is// at least the minimum of (a[i], a[j])function getPairsCount(arr, n){ // Stores the resultant count of pairs let count = 0; // Iterate over the range [0, n] for(let i = 0; i < n; i++) { // Iterate from arr[i] - (i%arr[i]) // till n with an increment // of arr[i] for(let j = arr[i] - (i % arr[i]); j < n; j += arr[i]) { // Count the possible pairs if (i < j && Math.abs(arr[i] - arr[j]) >= Math.min(arr[i], arr[j])) { count++; } } } // Return the total count return count;}// Driver Codelet arr = [ 1, 2, 2, 3 ];let N = arr.length;document.write(getPairsCount(arr, N));// This code is contributed by sanjoy_62</script> |
3
Time Complexity: O(N*log N)
Auxiliary Space: O(1)


