# Count pairs from a given array whose sum lies from a given range

Given an array **arr[]** consisting of **N** integers and two integers** L **and** R**, the task is to count the number of pairs whose sum lies in the range **[L, R]**.

**Examples:**

{5, 1, 2}, L = 4, R = 7Input:arr[] =Output:2Explanation:

The pairs satisfying the necessary conditions are as follows:

(5, 1):Sum = 5 + 1 = 6, which lies in the range [4, 7].(5, 2):Sum = 5 + 2 = 7, which lies in the range [4, 7].Therefore, the count of such pairs is 2.

{5, 1, 2, 4, 3}, L = 5, R = 8Input:arr[] =Output:7

**Naive Approach:** The simplest approach to solve the given problem is to generate all possible pairs and count those pairs whose sum lies over the range** [L, R]**. After checking for all the pairs, print the total count of pairs.

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can also be optimized by sorting the array then performing a Binary Search to find the number of elements for each array element **arr[i]** such that the sum will not exceed the given range. Follow the steps to solve the problem:

- Sort the array
**arr[]**in increasing order. - Initialize variables say,
**right**as**N – 1**and**count**as**0**to store numbers of pairs whose sum lies over the range**[L, R]**. - Iterate until the
**right**is greater than**0**and perform the following steps:- Find the starting index of the element whose sum with
**arr[right]**is greater than or equal to**L**, and store it in a variable, say**start**. - Find the ending index of the element before
**arr[right]**whose sum with**arr[right]**is less than or equal to**R**, and then store it in a variable, say**end**. - If the
**end**is greater than equal to the**start**, then add**(end – start + 1)**to the**count**representing the number of elements whose sum with the current element lies over the range**[L, R]**and then decrement**right**by**1**.

- Find the starting index of the element whose sum with
- After completing the above steps, print the value of
**count**as the result.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count pairs whose` `// sum lies over the range [L, R]` `int` `countPairSum(` `int` `arr[], ` `int` `L,` ` ` `int` `R, ` `int` `N)` `{` ` ` `// Sort the given array` ` ` `sort(arr, arr + N);` ` ` `int` `right = N - 1, count = 0;` ` ` `// Iterate until right > 0` ` ` `while` `(right > 0) {` ` ` `// Starting index of element` ` ` `// whose sum with arr[right] >= L` ` ` `auto` `it1` ` ` `= lower_bound(arr, arr + N,` ` ` `L - arr[right]);` ` ` `int` `start = it1 - arr;` ` ` `// Ending index of element` ` ` `// whose sum with arr[right] <= R` ` ` `auto` `it2` ` ` `= upper_bound(arr, arr + N,` ` ` `R - arr[right]);` ` ` `--it2;` ` ` `int` `end = it2 - arr;` ` ` `// Update the value of end` ` ` `end = min(end, right - 1);` ` ` `// Add the count of elements` ` ` `// to the variable count` ` ` `if` `(end - start >= 0) {` ` ` `count += (end - start + 1);` ` ` `}` ` ` `right--;` ` ` `}` ` ` `// Return the value of count` ` ` `return` `count;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 5, 1, 2, 4, 3 };` ` ` `int` `L = 5, R = 8;` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << countPairSum(arr, L, R, N);` ` ` `return` `0;` `}` |

## Python3

`# Python3 program for the above approach` `from` `bisect ` `import` `bisect_left, bisect_right` `# Function to count pairs whose` `# sum lies over the range [L, R]` `def` `countPairSum(arr, L, R, N):` ` ` ` ` `# Sort the given array` ` ` `arr.sort()` ` ` `right ` `=` `N ` `-` `1` ` ` `count ` `=` `0` ` ` `# Iterate until right > 0` ` ` `while` `(right > ` `0` `):` ` ` `# Starting index of element` ` ` `# whose sum with arr[right] >= L` ` ` `it1 ` `=` `bisect_left(arr, L ` `-` `arr[right])` ` ` `start ` `=` `it1` ` ` `# Ending index of element` ` ` `# whose sum with arr[right] <= R` ` ` `it2 ` `=` `bisect_right(arr, R ` `-` `arr[right])` ` ` `it2 ` `-` `=` `1` ` ` `end ` `=` `it2` ` ` `# Update the value of end` ` ` `end ` `=` `min` `(end, right ` `-` `1` `)` ` ` `# Add the count of elements` ` ` `# to the variable count` ` ` `if` `(end ` `-` `start >` `=` `0` `):` ` ` `count ` `+` `=` `(end ` `-` `start ` `+` `1` `)` ` ` `right ` `-` `=` `1` ` ` `# Return the value of count` ` ` `return` `count` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `arr ` `=` `[ ` `5` `, ` `1` `, ` `2` `, ` `4` `, ` `3` `]` ` ` `L ` `=` `5` ` ` `R ` `=` `8` ` ` `N ` `=` `len` `(arr)` ` ` ` ` `print` `(countPairSum(arr, L, R, N))` ` ` `# This code is contributed by SURENDRA_GANGWAR` |

**Output:**

7

**Time Complexity:** O(N*log N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.