# Longest subarray having average greater than or equal to x

Given an array of integers and an integer x. Find length of maximum size subarray having average of integers greater than or equal to x.

Examples:

Input : arr[] = {-2, 1, 6, -3}, x = 3 Output : 2 Longest subarray is {1, 6} having average 3.5 greater than x = 3. Input : arr[] = {2, -3, 3, 2, 1}, x = 2 Output : 3 Longest subarray is {3, 2, 1} having average 2 equal to x = 2.

A **simple** solution is to one by one consider each subarray and find its average. If average is greater than or equal to x, then compare length of subarray with maximum length found so far. Time complexity of this solution is O(n^{2}).

An **efficient** solution is to use binary search and prefix sum. Let the required Longest subarray be arr[i..j] and its length is l = j-i+1. Thus, mathematically it can be written as:

(Σ (arr[i..j]) / l ) ≥ x

==> (Σ (arr[i..j]) / l) – x ≥ 0

==> (Σ (arr[i..j]) – x*l) / l ≥ 0 …(1)

The equation (1) is equivalent to subtracting x from each element of subarray and then taking average of resultant subarray. So the resultant subarray can be obtained by subtracting x from each element of array. Let the updated subarray is arr1. Equation (1) can be further simplified as:

Σ (arr1[i..j]) / l ≥ 0

==> Σ (arr1[i..j]) ≥ 0 …(2)

Equation (2) is simply Longest subarray having sum greater than or equal to zero. The Longest subarray having sum greater than or equal to zero can be found by method discussed in following article:

Longest subarray having sum greater than k.

The step wise algo is:

1. Subtract x from each element of array.

2. Find Longest subarray having sum greater than or equal to zero in updated array using prefix sum and binary search.

Below is the implementation of above approach:

`// CPP program to find Longest subarray ` `// having average greater than or equal ` `// to x. ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Comparison function used to sort preSum vector. ` `bool` `compare(` `const` `pair<` `int` `, ` `int` `>& a, ` `const` `pair<` `int` `, ` `int` `>& b) ` `{ ` ` ` `if` `(a.first == b.first) ` ` ` `return` `a.second < b.second; ` ` ` ` ` `return` `a.first < b.first; ` `} ` ` ` `// Function to find index in preSum vector upto which ` `// all prefix sum values are less than or equal to val. ` `int` `findInd(vector<pair<` `int` `, ` `int` `> >& preSum, ` `int` `n, ` `int` `val) ` `{ ` ` ` ` ` `// Starting and ending index of search space. ` ` ` `int` `l = 0; ` ` ` `int` `h = n - 1; ` ` ` `int` `mid; ` ` ` ` ` `// To store required index value. ` ` ` `int` `ans = -1; ` ` ` ` ` `// If middle value is less than or equal to ` ` ` `// val then index can lie in mid+1..n ` ` ` `// else it lies in 0..mid-1. ` ` ` `while` `(l <= h) { ` ` ` `mid = (l + h) / 2; ` ` ` `if` `(preSum[mid].first <= val) { ` ` ` `ans = mid; ` ` ` `l = mid + 1; ` ` ` `} ` ` ` `else` ` ` `h = mid - 1; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Function to find Longest subarray having average ` `// greater than or equal to x. ` `int` `LongestSub(` `int` `arr[], ` `int` `n, ` `int` `x) ` `{ ` ` ` `int` `i; ` ` ` ` ` `// Update array by subtracting x from ` ` ` `// each element. ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `arr[i] -= x; ` ` ` ` ` `// Length of Longest subarray. ` ` ` `int` `maxlen = 0; ` ` ` ` ` `// Vector to store pair of prefix sum ` ` ` `// and corresponding ending index value. ` ` ` `vector<pair<` `int` `, ` `int` `> > preSum; ` ` ` ` ` `// To store current value of prefix sum. ` ` ` `int` `sum = 0; ` ` ` ` ` `// To store minimum index value in range ` ` ` `// 0..i of preSum vector. ` ` ` `int` `minInd[n]; ` ` ` ` ` `// Insert values in preSum vector. ` ` ` `for` `(i = 0; i < n; i++) { ` ` ` `sum = sum + arr[i]; ` ` ` `preSum.push_back({ sum, i }); ` ` ` `} ` ` ` ` ` `sort(preSum.begin(), preSum.end(), compare); ` ` ` ` ` `// Update minInd array. ` ` ` `minInd[0] = preSum[0].second; ` ` ` ` ` `for` `(i = 1; i < n; i++) { ` ` ` `minInd[i] = min(minInd[i - 1], preSum[i].second); ` ` ` `} ` ` ` ` ` `sum = 0; ` ` ` `for` `(i = 0; i < n; i++) { ` ` ` `sum = sum + arr[i]; ` ` ` ` ` `// If sum is greater than or equal to 0, ` ` ` `// then answer is i+1. ` ` ` `if` `(sum >= 0) ` ` ` `maxlen = i + 1; ` ` ` ` ` `// If sum is less than 0, then find if ` ` ` `// there is a prefix array having sum ` ` ` `// that needs to be added to current sum to ` ` ` `// make its value greater than or equal to 0. ` ` ` `// If yes, then compare length of updated ` ` ` `// subarray with maximum length found so far. ` ` ` `else` `{ ` ` ` `int` `ind = findInd(preSum, n, sum); ` ` ` `if` `(ind != -1 && minInd[ind] < i) ` ` ` `maxlen = max(maxlen, i - minInd[ind]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `maxlen; ` `} ` ` ` `// Driver code. ` `int` `main() ` `{ ` ` ` `int` `arr[] = { -2, 1, 6, -3 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `int` `x = 3; ` ` ` ` ` `cout << LongestSub(arr, n, x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity: ** O(nlogn)

**Auxiliary Space: ** O(n)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Longest subarray having average greater than or equal to x | Set-2
- Longest Subarray with Sum greater than Equal to Zero
- Length of longest subarray with product greater than or equal to 0
- Longest Subarray with first element greater than or equal to Last element
- Longest subarray in which all elements are greater than K
- Longest subarray in which absolute difference between any two element is not greater than X
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Longest subarray with elements having equal modulo K
- Longest subarray having difference in the count of 1's and 0's equal to k
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Length of longest Subarray with equal number of odd and even elements
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Find the subarray with least average
- Find maximum average subarray of k length
- Longest subsequence whose average is less than K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.