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:

- When the length of the array is
**0**. - When all the elements in the array are equal.
- 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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Longest Subarray with first element greater than or equal to Last element
- Length of longest subarray with product greater than or equal to 0
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Maximize the number of indices such that element is greater than element to its left
- Maximum length of subarray such that all elements are equal in the subarray
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Longest subarray having average greater than or equal to x
- Longest subarray having average greater than or equal to x | Set-2
- Longest Subarray with Sum greater than Equal to Zero
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Smallest subarray having an element with frequency greater than that of other elements
- Maximum element in an array such that its previous and next element product is maximum
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Length of Smallest Subsequence such that sum of elements is greater than equal to 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.