Open In App

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

Last Updated : 31 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to find the required subarray
function 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>


Output: 

2 4

 

Time Complexity: O(N)
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads