Count pairs in a sorted array whose product is less than k
Last Updated :
11 Jul, 2025
Given a sorted integer array and number k, the task is to count pairs in an array whose product is less than x.
Examples:
Input: A = {2, 3, 5, 6}, k = 16
Output: 4
Pairs having product less than 16: (2, 3), (2, 5), (2, 6), (3, 5)
Input: A = {2, 3, 4, 6, 9}, k = 20
Output: 6
Pairs having product less than 20: (2, 3), (2, 4), (2, 6), (2, 9), (3, 4), (3, 6)
A simple solution of this problem run two loops to generate all pairs and one by one and check if current pair’s product is less than x or not.
An Efficient solution of this problem is take initial and last value of index in l and r variable. Consider below two cases:
- Case-I:
- Lets consider i < j and A[i]*A[j] < k then we can say that A[i]*A[j-1] < k as A[j-1] < A[j] for a sorted array,
- Similarly A[i]*A[j-2] < k, A[i]*A[j-3] < k, ....., A[i]*A[i+1] < k.
- Case-II:
- Lets consider i k then we can say that A[i]*A[j+1] > k as A[j+1] > A[j] for a sorted array,
- similarly A[i]*A[j+2] > k, A[i]*A[j+3] > k, ....., A[i]*A[n-1] > k.
Above problem is similar to Count pairs in a sorted array whose sum is less than x, the only thing that is different is to find the product of pairs instead of sum.
Below is the algorithm to solve this problem:
1) Initialize two variables l and r to find the candidate
elements in the sorted array.
(a) l = 0
(b) r = n - 1
2) Initialize : result = 0
2) Loop while l < r.
// If current left and current
// right have product smaller than x,
// the all elements from l+1 to r
// form a pair with current
(a) If (arr[l] * arr[r] < x)
result = result + (r - l)
l++;
(b) Else
r--;
3) Return result
Below is the implementation of the above algorithm:
C++
// C++ program to find number of pairs with
// product less than k in a sorted array
#include <bits/stdc++.h>
using namespace std;
// Function to count the pairs
int fun(int A[], int n, int k)
{
// count to keep count of
// number of pairs with product
// less than k
int count = 0;
int i = 0;
int j = n - 1;
// Traverse the array
while (i < j) {
// If product is less than k
// then count that pair
// and increment 'i'
if (A[i] * A[j] < k) {
count += (j - i);
i++;
}
// Else decrement 'j'
else {
j--;
}
}
// Return count of pairs
return count;
}
// Driver code
int main()
{
int A[] = { 2, 3, 4, 6, 9 };
int n = sizeof(A) / sizeof(int);
int k = 20;
cout << "Number of pairs with product less than "
<< k << " = " << fun(A, n, k) << endl;
return 0;
}
Java
// Java program to find number
// of pairs with product less
// than k in a sorted array
class GFG
{
// Function to count the pairs
static int fun(int A[],
int n, int k)
{
// count to keep count of
// number of pairs with
// product less than k
int count = 0;
int i = 0;
int j = n - 1;
// Traverse the array
while (i < j)
{
// If product is less than
// k then count that pair
// and increment 'i'
if (A[i] * A[j] < k)
{
count += (j - i);
i++;
}
// Else decrement 'j'
else
{
j--;
}
}
// Return count of pairs
return count;
}
// Driver code
public static void main(String args[])
{
int A[] = {2, 3, 4, 6, 9};
int n = A.length;
int k = 20;
System.out.println("Number of pairs with " +
"product less than 20 = " +
fun(A, n, k));
}
}
// This code is contributed
// by Kirti_Mangal
Python
# Python program to find number of pairs with
# product less than k in a sorted array
def fun(A, k):
# count to keep count of number
# of pairs with product less than k
count = 0
n = len(A)
# Left pointer pointing to leftmost part
i = 0
# Right pointer pointing to rightmost part
j = n-1
# While left and right pointer don't meet
while i < j:
if A[i]*A[j] < k:
count += (j-i)
# Increment the left pointer
i+= 1
else:
# Decrement the right pointer
j-= 1
return count
# Driver code to test above function
A = [2, 3, 4, 6, 9]
k = 20
print("Number of pairs with product less than ",
k, " = ", fun(A, k))
C#
// C# program to find number
// of pairs with product less
// than k in a sorted array
using System;
class GFG
{
// Function to count the pairs
static int fun(int []A,
int n, int k)
{
// count to keep count of
// number of pairs with
// product less than k
int count = 0;
int i = 0;
int j = n - 1;
// Traverse the array
while (i < j)
{
// If product is less than
// k then count that pair
// and increment 'i'
if (A[i] * A[j] < k)
{
count += (j - i);
i++;
}
// Else decrement 'j'
else
{
j--;
}
}
// Return count of pairs
return count;
}
// Driver code
public static void Main()
{
int []A = {2, 3, 4, 6, 9};
int n = A.Length;
int k = 20;
Console.WriteLine("Number of pairs with " +
"product less than 20 = " +
fun(A, n, k));
}
}
// This code is contributed
// by Subhadeep
PHP
<?php
// PHP program to find number of
// pairs with product less than k
// in a sorted array
// Function to count the pairs
function fun($A, $n, $k)
{
// count to keep count of
// number of pairs with product
// less than k
$count = 0;
$i = 0;
$j = ($n - 1);
// Traverse the array
while ($i < $j)
{
// If product is less than k
// then count that pair
// and increment 'i'
if ($A[$i] * $A[$j] < $k)
{
$count += ($j - $i);
$i++;
}
// Else decrement 'j'
else
{
$j--;
}
}
// Return count of pairs
return $count;
}
// Driver code
$A = array( 2, 3, 4, 6, 9 );
$n = sizeof($A);
$k = 20;
echo "Number of pairs with product less than ",
$k , " = " , fun($A, $n, $k) , "\n";
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to find number
// of pairs with product less
// than k in a sorted array
// Function to count the pairs
function fun(A, n, k)
{
// count to keep count of
// number of pairs with
// product less than k
let count = 0;
let i = 0;
let j = n - 1;
// Traverse the array
while (i < j)
{
// If product is less than
// k then count that pair
// and increment 'i'
if (A[i] * A[j] < k)
{
count += (j - i);
i++;
}
// Else decrement 'j'
else
{
j--;
}
}
// Return count of pairs
return count;
}
let A = [2, 3, 4, 6, 9];
let n = A.length;
let k = 20;
document.write("Number of pairs with " +
"product less than 20 = " +
fun(A, n, k));
</script>
OutputNumber of pairs with product less than 20 = 6
Complexity Analysis:
- Time Complexity: O(N), where N is the size of the given array.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach 2: Binary Search:
This problem can be solved using binary search. We can fix one element of the pair and then use binary search to find the maximum index of the second element such that their product is less than k. We can repeat this process for all elements of the array and sum up the counts to get the total number of pairs with product less than k.
In this code, we use a nested loop to traverse the array and fix one element of the pair. We then use binary search to find the maximum index of the second element such that their product is less than k. We count the number of pairs for each fixed element and sum up the counts to get the total number of pairs with product less than k.
Here is the code to solve this problem using binary search in C++:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to count the pairs
int fun(int A[], int n, int k)
{
int count = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
int lo = i + 1;
int hi = n - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (A[i] * A[mid] < k) {
// All elements from lo to mid
// will satisfy the condition
count += (mid - lo + 1);
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
}
// Return count of pairs
return count;
}
// Driver code
int main()
{
int A[] = { 2, 3, 4, 6, 9 };
int n = sizeof(A) / sizeof(int);
int k = 20;
cout << "Number of pairs with product less than " << k
<< " = " << fun(A, n, k) << endl;
return 0;
}
Java
// GFG
// Java code for this approach
import java.util.*;
public class Main {
// Function to count the pairs
public static int fun(int[] A, int n, int k)
{
int count = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
int lo = i + 1;
int hi = n - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (A[i] * A[mid] < k) {
// All elements from lo to mid
// will satisfy the condition
count += (mid - lo + 1);
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
}
// Return count of pairs
return count;
}
// Driver code
public static void main(String[] args)
{
int[] A = { 2, 3, 4, 6, 9 };
int n = A.length;
int k = 20;
System.out.println(
"Number of pairs with product less than " + k
+ " = " + fun(A, n, k));
}
}
// This code is written by sundaram
Python3
# Function to count the pairs
def fun(A, n, k):
count = 0
# Traverse the array
for i in range(n):
lo = i + 1
hi = n - 1
while lo <= hi:
mid = lo + (hi - lo) // 2
if A[i] * A[mid] < k:
# All elements from lo to mid will satisfy the condition
count += (mid - lo + 1)
lo = mid + 1
else:
hi = mid - 1
# Return count of pairs
return count
# Driver code
A = [2, 3, 4, 6, 9]
n = len(A)
k = 20
print("Number of pairs with product less than", k, "=", fun(A, n, k))
C#
using System;
using System.Collections.Generic;
public class MainClass {
// Function to count the pairs
public static int Fun(int[] A, int n, int k)
{
int count = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
int lo = i + 1;
int hi = n - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (A[i] * A[mid] < k) {
// All elements from lo to mid
// will satisfy the condition
count += (mid - lo + 1);
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
}
// Return count of pairs
return count;
}
// Driver code
public static void Main()
{
int[] A = { 2, 3, 4, 6, 9 };
int n = A.Length;
int k = 20;
Console.WriteLine(
"Number of pairs with product less than " + k
+ " = " + Fun(A, n, k));
}
}
JavaScript
// Function to count the pairs
function fun(A, n, k) {
let count = 0;
// Traverse the array
for (let i = 0; i < n; i++) {
let lo = i + 1;
let hi = n - 1;
while (lo <= hi) {
let mid = lo + Math.floor((hi - lo) / 2);
if (A[i] * A[mid] < k) {
// All elements from lo to mid
// will satisfy the condition
count += (mid - lo + 1);
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
}
// Return count of pairs
return count;
}
// Driver code
let A = [2, 3, 4, 6, 9];
let n = A.length;
let k = 20;
console.log(`Number of pairs with product less than ${k} = ${fun(A, n, k)}`);
OutputNumber of pairs with product less than 20 = 6
Complexity Analysis:
Time Complexity: O(NLogN), where N is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Count pairs from a given array whose product lies in a given range Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i < j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: V
13 min read
Count pairs whose products exist in array Given an array, count those pair whose product value is present in array. Examples: Input : arr[] = {6, 2, 4, 12, 5, 3}Output : 3 All pairs whose product exist in array (6 , 2) (2, 3) (4, 3) Input : arr[] = {3, 5, 2, 4, 15, 8}Output : 2 A Simple solution is to generate all pairs of given array and c
15+ min read
Count of pairs in Array whose product is divisible by K Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose product is divisible by K. Examples : Input: A[] = [1, 2, 3, 4, 5], K = 2Output: 7Explanation: The 7 pairs of indices whose corresponding products are divisible by 2 are(0, 1), (0, 3), (1, 2)
9 min read
Count pairs in Array whose product is divisible by K Given a array vec and an integer K, count the number of pairs (i, j) such that vec[i]*vec[j] is divisible by K where i<j. Examples: Input: vec = {1, 2, 3, 4, 5, 6}, K = 4Output: 6Explanation: The pairs of indices (0, 3), (1, 3), (2, 3), (3, 4), (3, 5) and (1, 5) satisfy the condition as their pro
11 min read
Count sub-arrays whose product is divisible by k Given an integer K and an array arr[], the task is to count all the sub-arrays whose product is divisible by K.Examples: Input: arr[] = {6, 2, 8}, K = 4 Output: 4 Required sub-arrays are {6, 2}, {6, 2, 8}, {2, 8}and {8}.Input: arr[] = {9, 1, 14}, K = 6 Output: 1 Naive approach: Run nested loops and
15+ min read
Count sub-arrays whose product is divisible by k Given an integer K and an array arr[], the task is to count all the sub-arrays whose product is divisible by K.Examples: Input: arr[] = {6, 2, 8}, K = 4 Output: 4 Required sub-arrays are {6, 2}, {6, 2, 8}, {2, 8}and {8}.Input: arr[] = {9, 1, 14}, K = 6 Output: 1 Naive approach: Run nested loops and
15+ min read