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

• Difficulty Level : Medium
• Last Updated : 23 Nov, 2021

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:

Input: arr[] = {5, 1, 2}, L = 4, R = 7
Output: 2
Explanation:
The pairs satisfying the necessary conditions are as follows:

1. (5, 1): Sum = 5 + 1 = 6, which lies in the range [4, 7].
2. (5, 2): Sum = 5 + 2 = 7, which lies in the range [4, 7].

Therefore, the count of such pairs is 2.

Input: arr[] = {5, 1, 2, 4, 3}, L = 5, R = 8
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(N2)
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.
• 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 ``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);``    ``cout << countPairSum(arr, L, R, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ``static` `int` `lowerBound(``int``[] a, ``int` `low, ``int` `high, ``int` `element){``        ``while``(low < high){``            ``int` `middle = low + (high - low)/``2``;``            ``if``(element > a[middle])``                ``low = middle + ``1``;``            ``else``                ``high = middle;``        ``}``        ``return` `low;``    ``}`  `    ``static` `int` `upperBound(``int``[] a, ``int` `low, ``int` `high, ``int` `element){``        ``while``(low < high){``            ``int` `middle = low + (high - low)/``2``;``            ``if``(a[middle] > element)``                ``high = middle;``            ``else``                ``low = middle + ``1``;``        ``}``        ``return` `low;``    ``}``// Function to count pairs whose``// sum lies over the range [L, R]``static` `int` `countPairSum(``int` `arr[], ``int` `L,``                 ``int` `R, ``int` `N)``{``    ``// Sort the given array``    ``Arrays.sort(arr);` `    ``int` `right = N - ``1``, count = ``0``;` `    ``// Iterate until right > 0``    ``while` `(right > ``0``) {` `        ``// Starting index of element``        ``// whose sum with arr[right] >= L``        ``int` `it1``            ``= lowerBound(arr, ``0``,N,``                          ``L - arr[right]);``       ``it1++;``        ``int` `start = it1 - arr[``0``];` `        ``// Ending index of element``        ``// whose sum with arr[right] <= R``        ``int` `it2``            ``= upperBound(arr, ``0``,N,``                          ``R - arr[right]);`  `        ``int` `end = it2 - arr[``0``];` `        ``// Update the value of end``        ``end = Math.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``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``5``, ``1``, ``2``, ``4``, ``3` `};``    ``int` `L = ``5``, R = ``8``;``    ``int` `N = arr.length;``    ``System.out.print(countPairSum(arr, L, R, N));` `}``}` `// This code is contributed by gauravrajput1`

## 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`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG {``    ``static` `int` `lowerBound(``int``[] a, ``int` `low, ``int` `high, ``int` `element) {``        ``while` `(low < high) {``            ``int` `middle = low + (high - low) / 2;``            ``if` `(element > a[middle])``                ``low = middle + 1;``            ``else``                ``high = middle;``        ``}``        ``return` `low;``    ``}` `    ``static` `int` `upperBound(``int``[] a, ``int` `low, ``int` `high, ``int` `element) {``        ``while` `(low < high) {``            ``int` `middle = low + (high - low) / 2;``            ``if` `(a[middle] > element)``                ``high = middle;``            ``else``                ``low = middle + 1;``        ``}``        ``return` `low;``    ``}` `    ``// Function to count pairs whose``    ``// sum lies over the range [L, R]``    ``static` `int` `countPairSum(``int` `[]arr, ``int` `L, ``int` `R, ``int` `N)``    ``{``      ` `        ``// Sort the given array``        ``Array.Sort(arr);` `        ``int` `right = N - 1, count = 0;` `        ``// Iterate until right > 0``        ``while` `(right > 0) {` `            ``// Starting index of element``            ``// whose sum with arr[right] >= L``            ``int` `it1 = lowerBound(arr, 0, N, L - arr[right]);``            ``it1++;``            ``int` `start = it1 - arr;` `            ``// Ending index of element``            ``// whose sum with arr[right] <= R``            ``int` `it2 = upperBound(arr, 0, N, R - arr[right]);` `            ``int` `end = it2 - arr;` `            ``// Update the value of end``            ``end = Math.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``    ``public` `static` `void` `Main(String[] args) {``        ``int` `[]arr = { 5, 1, 2, 4, 3 };``        ``int` `L = 5, R = 8;``        ``int` `N = arr.Length;``        ``Console.Write(countPairSum(arr, L, R, N));` `    ``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``

Output:

`7`

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

My Personal Notes arrow_drop_up