# Subarray with difference between maximum and minimum element greater than or equal to its length

Given an array arr[], the task is to find a subarray with the difference between the maximum and the minimum element is greater than or equal to the length of subarray. If no such subarray exists then print -1.

Examples:

Input: arr[] = {3, 7, 5, 1}
Output: 3 7
|3 – 7| > length({3, 7}) i.e. 4 ≥ 2

Input: arr[] = {1, 2, 3, 4, 5}
Output: -1
There is no such subarray that meets the criteria.

Naive approach: Find All the subarray that are possible with at least two elements and then check for each of the subarrays that satisfy the given condition i.e. max(subarray) – min(subarray) ≥ len(subarray)

Efficient approach: Find the subarrays of length 2 where the absolute difference between the only two elements is greater than or equal to 2. This will cover almost all the cases because there are only three cases when there is no such subarray:

1. When the length of the array is 0.
2. When all the elements in the array are equal.
3. When every two consecutive elements in the array have an absolute difference either 0 or 1.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the required subarray ` `void` `findSubArr(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// For every two consecutive element subarray ` `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` ` `  `        ``// If the current pair of consecutive ` `        ``// elements satisfies the given condition ` `        ``if` `(``abs``(arr[i] - arr[i + 1]) >= 2) { ` `            ``cout << arr[i] << ``" "` `<< arr[i + 1]; ` `            ``return``; ` `        ``} ` `    ``} ` ` `  `    ``// No such subarray found ` `    ``cout << -1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 4, 6, 7 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``findSubArr(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GFG  ` `{ ` `     `  `    ``// Function to find the required subarray  ` `    ``static` `void` `findSubArr(``int` `arr[], ``int` `n)  ` `    ``{  ` `     `  `        ``// For every two consecutive element subarray  ` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++)  ` `        ``{  ` `     `  `            ``// If the current pair of consecutive  ` `            ``// elements satisfies the given condition  ` `            ``if` `(Math.abs(arr[i] - arr[i + ``1``]) >= ``2``)  ` `            ``{  ` `                ``System.out.print(arr[i] + ``" "` `+ arr[i + ``1``]);  ` `                ``return``;  ` `            ``}  ` `        ``}  ` `     `  `        ``// No such subarray found  ` `        ``System.out.print(-``1``);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{  ` `        ``int` `arr[] = { ``1``, ``2``, ``4``, ``6``, ``7` `};  ` `        ``int` `n = arr.length;  ` `     `  `        ``findSubArr(arr, n);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to find the required subarray ` `def` `findSubArr(arr, n) : ` ` `  `    ``# For every two consecutive element subarray ` `    ``for` `i ``in` `range``(n ``-` `1``) : ` ` `  `        ``# If the current pair of consecutive ` `        ``# elements satisfies the given condition ` `        ``if` `(``abs``(arr[i] ``-` `arr[i ``+` `1``]) >``=` `2``) : ` `            ``print``(arr[i] ,arr[i ``+` `1``],end``=``""); ` `            ``return``; ` ` `  `    ``# No such subarray found ` `    ``print``(``-``1``); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `    ``arr ``=` `[ ``1``, ``2``, ``4``, ``6``, ``7` `]; ` `    ``n ``=` `len``(arr); ` ` `  `    ``findSubArr(arr, n); ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to find the required subarray  ` `    ``static` `void` `findSubArr(``int` `[]arr, ``int` `n)  ` `    ``{  ` `     `  `        ``// For every two consecutive element subarray  ` `        ``for` `(``int` `i = 0; i < n - 1; i++)  ` `        ``{  ` `     `  `            ``// If the current pair of consecutive  ` `            ``// elements satisfies the given condition  ` `            ``if` `(Math.Abs(arr[i] - arr[i + 1]) >= 2)  ` `            ``{  ` `                ``Console.Write(arr[i] + ``" "` `+ arr[i + 1]);  ` `                ``return``;  ` `            ``}  ` `        ``}  ` `     `  `        ``// No such subarray found  ` `        ``Console.Write(-1);  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[]arr = { 1, 2, 4, 6, 7 };  ` `        ``int` `n = arr.Length;  ` `     `  `        ``findSubArr(arr, n);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```2 4
```

Time Complexity: O(N)

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : AnkitRai01

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.