Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K

  • Last Updated : 10 Nov, 2021

Given an array arr[] of size N, and an integer K, the task is to count the number of pairs from the given array whose quotient of division of the larger element by the smaller element in the pair does not exceed K.

Examples:

Input: arr[] = {3, 12, 5, 13}, K=2
Output: 2
Explanation: Pairs satisfying the given conditions are (3, 5) and (12, 13).

Input: arr[] = {2, 3, 9, 5}, K=2
Output: 3
Explanation: Pairs satisfying the given conditions are (2, 3), (3, 5) and (5, 9).

Naive Approach: The simplest approach is to generate all possible pairs from the given array, and for each pair, check if required conditions are satisfied. For the pairs satisfying the condition, increment count by 1. After checking for all the pairs, print the count obtained. 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to sort the array in ascending order and then for each array element arr[i], use Binary Search to find the index, say high, of the element which is just greater than K * arr[i]. All the elements in the range [i + 1, high – 1] will form a pair with arr[i]
Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
void countPairs(int arr[], int n, int k)
{
    // Sort the array in ascending order
    sort(arr, arr + n);
 
    // Store the required result
    int ans = 0;
 
    // Traverse the array
    for (int i = 0; i < n - 1; i++) {
 
        // Store the upper bound for
        // the current array element
        int high
            = upper_bound(arr, arr + n, k * arr[i]) - arr;
 
        // Update the number of pairs
        ans += high - i - 1;
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given array, arr[]
    int arr[] = { 2, 3, 9, 5 };
 
    // Store the size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int k = 2;
   
    countPairs(arr, n, k);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
public static int upper_bound(int arr[], int key)
{
    int l = -1, r = arr.length;
    while (l + 1 < r)
    {
        int m = (l + r) >>> 1;
        if (arr[m] <= key)
            l = m;
        else
            r = m;
    }
    return l + 1;
}
 
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
static void countPairs(int arr[], int n, int k)
{
     
    // Sort the array in ascending order
    Arrays.sort(arr);
 
    // Store the required result
    int ans = 0;
 
    // Traverse the array
    for(int i = 0; i < n - 1; i++)
    {
         
        // Store the upper bound for
        // the current array element
        int high = upper_bound(arr, k * arr[i]);
 
        // Update the number of pairs
        ans += high - i - 1;
    }
 
    // Print the result
    System.out.println(ans);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array, arr[]
    int arr[] = { 2, 3, 9, 5 };
 
    // Store the size of the array
    int n = arr.length;
 
    int k = 2;
 
    countPairs(arr, n, k);
}
}
 
// This code is contributed by Kingash

Python3




# Python3 program for the above approach
from bisect import bisect_right
 
#  Function to count the number
#  having quotient of division
#  of larger element by the smaller
#  element in the pair not exceeding K
def countPairs(arr, n, k) :
     
    #  Sort the array in ascending order
    arr.sort()
 
    #  Store the required result
    ans = 0
 
    #  Traverse the array
    for i in range(n - 1):
 
        #  Store the upper bound for
        #  the current array element
        high = bisect_right(arr, k * arr[i])
 
        #  Update the number of pairs
        ans += high - i - 1
     
    #  Print result
    print(ans)
 
#  Driver Code
 
#  Given array, arr[]
arr = [ 2, 3, 9, 5 ]
 
#  Store the size of the array
n = len(arr)
k = 2
countPairs(arr, n, k)
 
# This code is contributed by sanjoy_62.

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
static int upper_bound(int []arr, int key)
{
    int l = -1, r = arr.Length;
    while (l + 1 < r)
    {
        int m = (l + r) >> 1;
        if (arr[m] <= key)
            l = m;
        else
            r = m;
    }
    return l + 1;
}
 
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
static void countPairs(int []arr, int n, int k)
{
     
    // Sort the array in ascending order
    Array.Sort(arr);
 
    // Store the required result
    int ans = 0;
 
    // Traverse the array
    for(int i = 0; i < n - 1; i++)
    {
         
        // Store the upper bound for
        // the current array element
        int high = upper_bound(arr, k * arr[i]);
 
        // Update the number of pairs
        ans += high - i - 1;
    }
 
    // Print the result
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main()
{
     
    // Given array, arr[]
    int []arr = { 2, 3, 9, 5 };
 
    // Store the size of the array
    int n = arr.Length;
    int k = 2;
    countPairs(arr, n, k);
}
}
 
// This code is contributed by SURENDRA_GANGWAR.

Javascript




<script>
 
// Javascript program for the above approach
 
function upper_bound(arr, key)
{
    let l = -1, r = arr.length;
    while (l + 1 < r)
    {
        let m = (l + r) >>> 1;
         
        if (arr[m] <= key)
            l = m;
        else
            r = m;
    }
    return l + 1;
}
  
// Function to count the number
// having quotient of division
// of larger element by the smaller
// element in the pair not exceeding K
function countPairs(arr, n, k)
{
     
    // Sort the array in ascending order
    arr.sort();
  
    // Store the required result
    let ans = 0;
  
    // Traverse the array
    for(let i = 0; i < n - 1; i++)
    {
         
        // Store the upper bound for
        // the current array element
        let high = upper_bound(arr, k * arr[i]);
  
        // Update the number of pairs
        ans += high - i - 1;
    }
  
    // Print the result
    document.write(ans);
}
 
// Driver code
 
// Given array, arr[]
let arr = [ 2, 3, 9, 5 ];
 
// Store the size of the array
let n = arr.length;
 
let k = 2;
 
countPairs(arr, n, k);
 
// This code is contributed by target_2   
 
</script>
Output: 
3

 

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

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!