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:Â
Â
- 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 of 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 subarrayvoid 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 codeint 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 subarraydef 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 codeif __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 |
Javascript
<script>Â
// JavaScript implementation of the approachÂ
// Function to find the required subarrayfunction findSubArr(arr, n) {Â
    // For every two consecutive element subarray    for (let 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) {            document.write(arr[i] + " " + arr[i + 1]);            return;        }    }Â
    // No such subarray found    document.write(-1);}Â
// Driver codeÂ
let arr = [1, 2, 4, 6, 7];let n = arr.lengthÂ
findSubArr(arr, n);Â
// This code is contributed by gfgkingÂ
</script> |
2 4
Â
Time Complexity: O(N)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



