Given an **array arr[]** of length **N**, tark is to find the length of longest subarray such that difference of its maximum element and minumum element is not more than an integer **K**

**Examples:**

Input:K = 3, arr[]= {1, 4, 5, 6, 9}

Output:3

Explanation:

Largest Subarray is {4, 5, 6}

Input:K = 4, arr[]= {1, 2, 3, 4, 5}

Output:5

Explanation:

Largest Subarray is {1, 2, 3, 4, 5}

**Naive approach:**

- Generate all subarray and find the minimum and maximum in the subarray.
- Calculate difference between minimum and maximum and if it is smaller or equal to K, then update answer

**Time Complexity: ** O(N^{3})

**Efficient approach:** The idea is to first sort the array, and then use binary search to optimize the approach.

- Sort the given array
- For every distinct element
**A[i]**in the array find the first element**A[j]**such that**(A[j]-A[i]) > K**. - For its implementation we use
**Binary search**or**lower_bound**and update ans each time as maximum of previous ans and difference of indixes.

Below is the implementation of the above approach:

`// C++ program to find longest ` `// subarray such that difference ` `// of max and min is at-most K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate longest ` `// subarray with above condition ` `int` `findLargestSubarray( ` ` ` `vector<` `int` `>& arr, ` ` ` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Sort the array ` ` ` `sort(arr.begin(), arr.end()); ` ` ` ` ` `int` `value1 = arr[0], value2 = 0; ` ` ` `int` `index1, index2, i, MAX; ` ` ` `index1 = index2 = 0; ` ` ` `i = 0, MAX = 0; ` ` ` ` ` `// Loop which will terminate ` ` ` `// when no further elements ` ` ` `// can be included in the subarray ` ` ` ` ` `while` `(index2 != N) { ` ` ` ` ` `// first value such that ` ` ` `// arr[index2] - arr[index1] > K ` ` ` `value2 = value1 + (K + 1); ` ` ` ` ` `// calculate its index using lower_bound ` ` ` `index2 = lower_bound(arr.begin(), ` ` ` `arr.end(), value2) ` ` ` `- arr.begin(); ` ` ` ` ` `// index2- index1 will give ` ` ` `// the accurate length ` ` ` `// of suarray then compare ` ` ` `// for MAX length and store ` ` ` `// in MAX variable ` ` ` `MAX = max(MAX, (index2 - index1)); ` ` ` ` ` `// change the index1 ` ` ` `// to next greater element ` ` ` `// than previous one ` ` ` `// and recalculate the value1 ` ` ` `index1 ` ` ` `= lower_bound( ` ` ` `arr.begin(), ` ` ` `arr.end(), arr[index1] + 1) ` ` ` `- arr.begin(); ` ` ` `value1 = arr[index1]; ` ` ` `} ` ` ` ` ` `// finally return answer MAX ` ` ` `return` `MAX; ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N, K; ` ` ` `N = 18; ` ` ` `K = 5; ` ` ` `vector<` `int` `> arr{ 1, 1, 1, 2, 2, ` ` ` `2, 2, 2, 3, ` ` ` `3, 3, 6, 6, 7, ` ` ` `7, 7, 7, 7 }; ` ` ` `cout << findLargestSubarray(arr, N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

15

**Time Complexity: ** O(N*log(N))

## Recommended Posts:

- Min and max length subarray having adjacent element difference atmost K
- Longest subarray such that the difference of max and min is at-most one
- Partition N into M parts such that difference between Max and Min part is smallest
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Longest subsequence such that absolute difference between every pair is atmost 1
- Longest Subarray having sum of elements atmost 'k'
- Length of Longest Subarray with same elements in atmost K increments
- Find the length of the longest subarray with atmost K occurrences of the integer X
- Minimum value of "max + min" in a subarray
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove exactly one element from the array such that max - min is minimum
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Remove minimum elements from the array such that 2*min becomes more than max
- Minimum difference between max and min of all K-size subsets
- k size subsets with maximum difference d between max and min
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Maximum average of a subarray of size of atleast X and atmost Y
- Maximize the maximum subarray sum after removing atmost one element
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Sum of width (max and min diff) of all Subsequences

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.