Thursday, July 29, 2021

Published July 29, 2021 by Anonymous with 0 comment

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:

  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++

#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;

}

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.


Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment