Count pairs from an array with absolute difference not less than the minimum element in the pair
Last Updated :
16 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int getPairsCount( int arr[], int n)
{
int count=0;
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++;
}
}
return count;
}
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.Arrays;
class Main
{
static int getPairsCount( int arr[], int n) {
int count = 0 ;
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++;
}
}
return count;
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 2 , 3 };
int N = arr.length;
System.out.println(getPairsCount(arr, N));
}
}
|
Python3
def getPairsCount(arr, n):
count = 0
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
return count
arr = [ 1 , 2 , 2 , 3 ]
N = len (arr)
print (getPairsCount(arr, N))
|
C#
using System;
class MainClass
{
static int GetPairsCount( int [] arr, int n)
{
int count = 0;
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++;
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 2, 3 };
int N = arr.Length;
Console.WriteLine(GetPairsCount(arr, N));
}
}
|
Javascript
function getPairsCount(arr, n) {
let count = 0;
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++;
}
}
}
return count;
}
const arr = [1, 2, 2, 3];
const N = arr.length;
console.log(getPairsCount(arr, N));
|
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++
#include <bits/stdc++.h>
using namespace std;
int getPairsCount( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = arr[i] - (i % arr[i]);
j < n; j += arr[i]) {
if (i < j
&& abs (arr[i] - arr[j])
>= min(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 1, 2, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getPairsCount(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int getPairsCount( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = arr[i] - (i % arr[i]); j < n;
j += arr[i]) {
if (i < j
&& Math.abs(arr[i] - arr[j])
>= Math.min(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 3 };
int N = arr.length;
System.out.println(getPairsCount(arr, N));
}
}
|
Python3
def getPairsCount(arr, n):
count = 0
for i in range (n):
for j in range (arr[i] - (i % arr[i]),n,arr[i]):
if (i < j and abs (arr[i] - arr[j]) > = min (arr[i], arr[j])):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 1 , 2 , 2 , 3 ]
N = len (arr)
print (getPairsCount(arr, N))
|
C#
using System;
class GFG{
static int getPairsCount( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = arr[i] - (i % arr[i]); j < n;
j += arr[i]) {
if (i < j
&& Math.Abs(arr[i] - arr[j])
>= Math.Min(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int [] arr = { 1, 2, 2, 3 };
int N = arr.Length;
Console.Write(getPairsCount(arr, N));
}
}
|
Javascript
<script>
function getPairsCount(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
{
for (let j = arr[i] - (i % arr[i]);
j < n; j += arr[i])
{
if (i < j && Math.abs(arr[i] - arr[j]) >=
Math.min(arr[i], arr[j]))
{
count++;
}
}
}
return count;
}
let arr = [ 1, 2, 2, 3 ];
let N = arr.length;
document.write(getPairsCount(arr, N));
</script>
|
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;
int getPairsCount( int arr[], int n)
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
int count = 0;
for ( int i = 0; i < n; i++) {
freq[arr[i]]--;
for ( int j = arr[i] - (i % arr[i]); j < n; j += arr[i]) {
if (i < j && abs (arr[i] - arr[j]) >= min(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
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 {
static int getPairsCount( int [] arr, int n) {
Map<Integer, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
freq.put(arr[i], freq.getOrDefault(arr[i], 0 ) + 1 );
}
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
freq.put(arr[i], freq.get(arr[i]) - 1 );
for ( int j = arr[i] - (i % arr[i]); j < n; j += arr[i]) {
if (i < j && Math.abs(arr[i] - arr[j]) >= Math.min(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 2 , 3 };
int n = arr.length;
System.out.println(getPairsCount(arr, n));
}
}
|
Python3
def getPairsCount(arr, n):
freq = {}
for i in range (n):
freq[arr[i]] = freq.get(arr[i], 0 ) + 1
count = 0
for i in range (n):
freq[arr[i]] - = 1
for j in range (arr[i] - (i % arr[i]), n, arr[i]):
if i < j and abs (arr[i] - arr[j]) > = min (arr[i], arr[j]):
count + = 1
return count
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
{
static int GetPairsCount( int [] arr, int n)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (freq.ContainsKey(arr[i]))
freq[arr[i]]++;
else
freq[arr[i]] = 1;
}
int count = 0;
for ( int i = 0; i < n; i++)
{
freq[arr[i]]--;
for ( int j = arr[i] - (i % arr[i]); j < n; j += arr[i])
{
if (i < j && Math.Abs(arr[i] - arr[j]) >= Math.Min(arr[i], arr[j]))
{
count++;
}
}
}
return count;
}
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) {
const freq = new Map();
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);
}
}
let count = 0;
for (let i = 0; i < n; i++) {
freq.set(arr[i], freq.get(arr[i]) - 1);
for (let j = arr[i] - (i % arr[i]); j < n; j += arr[i]) {
if (i < j && Math.abs(arr[i] - arr[j]) >= Math.min(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
const arr = [1, 2, 2, 3];
const N = arr.length;
console.log(getPairsCount(arr, N));
|
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...