- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count pairs in a sorted array whose sum is less than x 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 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)

#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[0]); 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)

#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[0]); 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

- Related Articles
- Count pairs in a sorted array whose product is less than k in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Count of pairs in an array whose sum is a perfect square in C++
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Count pairs with sum as a prime number and less than n in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs with bitwise OR less than Max in C++
- Count ordered pairs with product less than N in C++
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Count number of distinct pairs whose sum exists in the given array in C++