Skip to content
Related Articles

Related Articles

Improve Article

Count pairs from an array with absolute difference not less than the minimum element in the pair

  • Difficulty Level : Medium
  • Last Updated : 11 Sep, 2021

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:

  1. (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.
  2. (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.
  3. (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 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>
Output: 
3

 

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :