Count pairs in a sorted array whose product is less than k in C++


We are given a sorted array of integer type elements and an integer variable x and the task is to form the pairs from the given array and calculate the product of elements in the pair and check whether the calculated product is less than k or not.

Input 

int arr[] = {2, 7, 1, 0, 8}, int k = 10

Output 

Count of pairs in a sorted array whose product is less than k are: 7

Explanation 

The pairs that can be formed from the given array are: (2, 7) = 14(greater than k), (2, 1) = 2(less than k), (2, 0) = 0(less than k), (2, 8) = 16(greater than k), (7, 1) = 7(less than k), (7, 0) = 0(less than k), (7, 8) = 56(greater than k), (1, 0) = 0(less than k), (1, 8) = 8(less than k), (0, 8) = 0(less than k). So, the count of pairs with sum less than k are 7.

Input 

int arr[] = {2, 4, 6, 8}, int k = 10

Output 

Count of pairs in a sorted array whose product is less than k are: 1

Explanation 

The pairs that can be formed from the given array are: (2, 4) = 8(less than k), (2, 6) = 12(greater than k), (2, 8) = 16(greater than k), (4, 6) = 24(greater than x), (4, 8) = 32(greater than k), (6, 8) = 48(greater than k). So, the count of pairs with products less than k is 1.

Approach used in the below program is as follows

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach.

  • Input an array of integer elements and calculate the size of an array and pass the data to the function

  • Declare a temporary variable count to store the count of pairs with the product less than k.

  • Start loop FOR from i to 0 till the size of an array

  • Inside the loop, start another loop FOR from j to i + 1 till the size of an array

  • Inside the loop calculate the product as arr[i] * arr[j] and check IF product < k then increment the count by 1.

  • Return the count

  • Print result.

Efficient approach

  • Input an array of integer elements and calculate the size of an array and pass the data to the function

  • Declare a temporary variable count to store the count of pairs with the sum less than x.

  • Set arr_0 as 0 and arr_1 as size-1

  • Start loop FOR from arr_0 till arr_1

  • Inside the loop, check IF arr[arr_0] * arr[arr_1] < x then set count as count + (arr_1 - arr_0) and increment arr_0++ ELSE decrement arr_1 by 1

  • Return the count

  • Print the result.

Example (naive approach)

 Live Demo

#include <iostream>
using namespace std;
int pair_product(int arr[], int size, int k){
   int count = 0;
   int product = 1;
   for(int i = 0 ; i<size ; i++){
      for(int j = i+1; j<size; j++){
         product = arr[i] * arr[j];
         if(product < k){
            count++;
         }
      }
   }
   return count;
}
int main(){
   int arr[] = {5, 8, 2, 1, 3};
   int size = sizeof(arr) / sizeof(arr[0]);
   int k = 10;
   cout<<"Count of pairs in a sorted array whose product is less than k are: "<<pair_product(arr, size, k);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of pairs in a sorted array whose product is less than k are: 5

Example (Efficient approach)

 Live Demo

#include <iostream>
using namespace std;
int pair_product(int arr[], int size, int k){
   int arr_0 = 0;
   int arr_1 = size-1;
   int count = 0;
   int product = 1;
   while(arr_0 < arr_1){
      product = arr[arr_0] * arr[arr_1];
      if (product < k){
         count = count + (arr_1 - arr_0);
         arr_0++;
      }
      else{
         arr_1--;
      }
   }
   return count;
}
int main(){
   int arr[] = {1, 3, 4, 2, 1};
   int size = sizeof(arr) / sizeof(arr[0]);
   int k = 5;
   cout<<"Count of pairs in a sorted array whose product is less than k are: "<<pair_product(arr, size, k);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of pairs in a sorted array whose product is less than k are: 10

Updated on: 02-Nov-2020

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements