- Related Questions & Answers
- Count pairs in a sorted array whose sum is less than x in C++
- Count ordered pairs with product less than N in C++
- Count all subsequences having product less than K in C++
- Subarray Product Less Than K in C++
- Count pairs in array whose sum is divisible by K in C++
- Count subarrays whose product is divisible by k in C++
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Count pairs with bitwise OR less than Max in C++
- Count of subarrays whose maximum element is greater than k in C++
- Count pairs whose products exist in array in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Count pairs from two linked lists whose product is equal to a given value in C++
- Count of alphabets having ASCII value less than and greater than k in C++
- Count of pairs in an array whose sum is a perfect square in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

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.

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

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

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

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

Advertisements