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.
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) { int count=0; //TO store the count. for ( int i=0;i<n;i++){ for ( int j=i+1;j<n;j++){ if ( abs (arr[i]-arr[j])>=min(arr[i],arr[j])) count++; // increasing the count when we found the pair } } // Return the total count return count; } // Driver Code int 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 approach import java.util.Arrays; class Main { // 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) { int count = 0 ; // To store the count for ( int i = 0 ; i < n; i++) { for ( int j = i + 1 ; j < n; j++) { if (Math.abs(arr[i] - arr[j]) >= Math.min(arr[i], arr[j])) count++; // increasing the count when we found the pair } } // 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 rishabmalhdijo |
Python3
# code def getPairsCount(arr, n): count = 0 # To store the count. for i in range (n): for j in range (i + 1 , n): if abs (arr[i] - arr[j]) > = min (arr[i], arr[j]): count + = 1 # Increasing the count when we found the pair # Return the total count return count arr = [ 1 , 2 , 2 , 3 ] N = len (arr) print (getPairsCount(arr, N)) |
C#
using System; class MainClass { // 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) { int count = 0; // To store the count for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { if (Math.Abs(arr[i] - arr[j]) >= Math.Min(arr[i], arr[j])) { count++; // increasing the count when we found the pair } } } // Return the total count return count; } // Driver Code public static void Main( string [] args) { int [] arr = { 1, 2, 2, 3 }; int N = arr.Length; Console.WriteLine(GetPairsCount(arr, N)); } } |
Javascript
// JavaScript code 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) { let count = 0; // to store the count. for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { if (Math.abs(arr[i] - arr[j]) >= Math.min(arr[i], arr[j])) { count++; // increasing the count when we found the pair } } } // Return the total count return count; } // Driver Code const arr = [1, 2, 2, 3]; const N = arr.length; console.log(getPairsCount(arr, N)); // The code is contributed by Arushi Goel. |
3
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 Code int 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 approach import 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 Code if __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 approach using 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 Code public 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 Code let 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)
Approach using DP:
The current implementation uses nested loops to iterate over the array elements and count the pairs that satisfy the given condition. However, we can optimize the solution by using a hash table or a frequency array to store the counts of each element in the array.
Here’s an updated version of the code that utilizes a hash table to achieve the same result:
C++
#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) { // Create a hash table to store the frequency of elements unordered_map< int , int > freq; // Iterate over the array and count the frequency of each element for ( int i = 0; i < n; i++) { freq[arr[i]]++; } // Stores the resultant count of pairs int count = 0; // Iterate over the array elements for ( int i = 0; i < n; i++) { // Decrement the frequency of the current element freq[arr[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 Code int main() { int arr[] = { 1, 2, 2, 3 }; int N = sizeof (arr) / sizeof (arr[0]); cout << getPairsCount(arr, N); return 0; } |
Java
import java.util.HashMap; import java.util.Map; public 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) { // Create a hash map to store the frequency of elements Map<Integer, Integer> freq = new HashMap<>(); // Iterate over the array and count the frequency of each element for ( int i = 0 ; i < n; i++) { freq.put(arr[i], freq.getOrDefault(arr[i], 0 ) + 1 ); } // Stores the resultant count of pairs int count = 0 ; // Iterate over the array elements for ( int i = 0 ; i < n; i++) { // Decrement the frequency of the current element freq.put(arr[i], freq.get(arr[i]) - 1 ); // 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)); } } |
Python3
# 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): # Create a dictionary (hash table) to store the frequency of elements freq = {} # Iterate over the array and count the frequency of each element for i in range (n): freq[arr[i]] = freq.get(arr[i], 0 ) + 1 # Stores the resultant count of pairs count = 0 # Iterate over the array elements for i in range (n): # Decrement the frequency of the current element freq[arr[i]] - = 1 # 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 Code if __name__ = = "__main__" : arr = [ 1 , 2 , 2 , 3 ] N = len (arr) print (getPairsCount(arr, N)) |
C#
using System; using System.Collections.Generic; namespace PairsCountExample { class Program { // 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) { // Create a dictionary to store the frequency of elements Dictionary< int , int > freq = new Dictionary< int , int >(); // Iterate over the array and count the frequency of each element for ( int i = 0; i < n; i++) { if (freq.ContainsKey(arr[i])) freq[arr[i]]++; else freq[arr[i]] = 1; } // Stores the resultant count of pairs int count = 0; // Iterate over the array elements for ( int i = 0; i < n; i++) { // Decrement the frequency of the current element freq[arr[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 static void Main( string [] args) { int [] arr = { 1, 2, 2, 3 }; int N = arr.Length; Console.WriteLine(GetPairsCount(arr, N)); Console.ReadLine(); } } } |
Javascript
function getPairsCount(arr, n) { // Create a Map (equivalent to Dictionary in C#) to store the frequency of elements const freq = new Map(); // Iterate over the array and count the frequency of each element for (let i = 0; i < n; i++) { if (freq.has(arr[i])) { freq.set(arr[i], freq.get(arr[i]) + 1); } else { freq.set(arr[i], 1); } } // Stores the resultant count of pairs let count = 0; // Iterate over the array elements for (let i = 0; i < n; i++) { // Decrement the frequency of the current element freq.set(arr[i], freq.get(arr[i]) - 1); // 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 Code const arr = [1, 2, 2, 3]; const N = arr.length; console.log(getPairsCount(arr, N)); |
3
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!