# Count pairs in a sorted array whose sum is less than x in C++

C++Server Side ProgrammingProgramming

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 sum of elements in the pair and check whether the calculated sum is less than x or not.

Input − int arr[] = {2, 7, 1, 0, 8}, int x = 8

Output − Count of pairs in a sorted array whose sum is less than x are − 4

Explanation − The pairs that can be formed from the given array are: (2, 7) = 9(greater than x), (2, 1) = 3(less than x), (2, 0) = 2(less than x), (2, 8) = 10(greater than x), (7, 1) = 8(equals to x), (7, 0) = 7(less than x), (7, 8) = 15(greater than x), (1, 0) = 1(less than x), (1, 8) = 8(equals to x), (0, 8) = 8(equals to x). So the pairs with the sum less than x are (4, 0) and (2, 2). So, the count of pairs with sum less than x are 4.

Input − int arr[] = {2, 4, 6, 8}, int x = 10

Output − Count of pairs in a sorted array whose sum is less than x are − 2

Explanation − The pairs that can be formed from the given array are: (2, 4) = 6(less than x), (2, 6) = 8(less than x), (2, 8) = 10(equals to x), (4, 6) = 10(equals to x), (4, 8) = 12(greater than x), (6, 8) = 14(greater than x). So, the count of pairs with sum less than x are 2.

## 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 sum less than x.

• 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 sum as arr[i] + arr[j] and check IF sum < x 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_sum(int arr[], int size, int x){
int count = 0;
int sum = 0;
for(int i = 0 ;i <size ; i++){
for(int j = i+1; j<size; j++){
sum = arr[i] + arr[j];
if(sum < x){
count++;
}
}
}
return count;
}
int main(){
int arr[] = {2, 7, 1, 0, 8};
int size = sizeof(arr) / sizeof(arr);
int x = 8;
cout<<"Count of pairs in a sorted array whose sum is less than x are: "<<pair_sum(arr, size, x);
return 0;
}

## Output

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

Count of pairs in a sorted array whose sum is less than x are: 4

## Example (Efficient approach)

Live Demo

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

## Output

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

Count of pairs in a sorted array whose sum is less than x are: 4