Longest subarray in which absolute difference between any two element is not greater than X

30 Jun, 2022
Given an integer array arr[] of size N and an integer X, the task is to find the longest sub-array where the absolute difference between any two elements is not greater than X. Examples:  
Input: arr = { 8, 4, 2, 6, 7 }, X = 4 Output: 4 2 6 Explanation: The sub-array described by index [1, 3], i.e, { 4, 2, 6 } contains no such difference of two elements which is greater than 4.Input: arr = { 15, 10, 1, 2, 4, 7, 2}, X = 5 Output: 2 4 7 2 Explanation: The sub-array described by indexes [3, 6], i.e, { 2, 4, 7, 2 } contains no such difference of two elements which is greater than 5.  
 
Naive Approach: Simple solution is to consider all subarrays one by one, find the maximum and minimum element of that sub-array and check if their difference is not greater than X. Among all such sub-arrays print the longest sub-array.Time Complexity: O(N3)Efficient Approach: The idea is to use the Sliding Window Technique to consider a sub-array and use a Map data structure to find the maximum and minimum element in that sub-array.  
At first the Start and End of the window points to the 0-th index.
At every iteration, the element at End is inserted in the Map if not already present or otherwise its count is incremented.
If the difference between the maximum and minimum element is not greater than X, then update the maximum length of the required sub-array and store the beginning of that sub-array in a variable.
Otherwise, increment the Start of the window until the difference between the maximum and minimum element is not greater than X.
When incrementing the Start, the size of the window decreases, remove the element at the Start from the Map if and only if the count of that element becomes zero.
Finally, print the sub-array with the longest length, and the absolute difference between any two elements is not greater than the X.Below is the implementation of the above approach:  
C++
Java
Python3
Javascript
// C++ program to find the longest sub-array// where the absolute difference between any// two elements is not greater than X #include <bits/stdc++.h>using namespace std; // Function that prints the longest sub-array// where the absolute difference between any// two element is not greater than Xvoid longestSubarray(int* A, int N, int X){    // Initialize a variable to store    // length of longest sub-array    int maxLen = 0;     // Initialize a variable to store the    // beginning of the longest sub-array    int beginning = 0;     // Initialize a map to store the maximum    // and the minimum elements for a given window    map<int, int> window;     // Initialize the window    int start = 0, end = 0;     // Loop through the array    for (; end < N; end++) {        // Increment the count of that        // element in the window        window[A[end]]++;         // Find the maximum and minimum element        // in the current window        auto minimum = window.begin()->first;        auto maximum = window.rbegin()->first;         // If the difference is not        // greater than X        if (maximum - minimum <= X) {            // Update the length of the longest            // sub-array and store the beginning            // of the sub-array            if (maxLen < end - start + 1) {                maxLen = end - start + 1;                beginning = start;            }        }        // Decrease the size of the window        else {            while (start < end) {                // Remove the element at start                window[A[start]]--;                 // Remove the element from the window                // if its count is zero                if (window[A[start]] == 0) {                     window.erase(window.find(A[start]));                }                // Increment the start of the window                start++;                 // Find the maximum and minimum element                // in the current window                auto minimum = window.begin()->first;                auto maximum = window.rbegin()->first;                 // Stop decreasing the size of window                // when difference is not greater                if (maximum - minimum <= X)                    break;            }        }    }     // Print the longest sub-array    for (int i = beginning; i < beginning + maxLen; i++)        cout << A[i] << " ";} // Driver Codeint main(){    int arr[] = { 15, 10, 1, 2, 4, 7, 2 }, X = 5;     int n = sizeof(arr) / sizeof(arr[0]);     longestSubarray(arr, n, X);     return 0;}
// JAVA program to find the longest sub-array// where the absolute difference between any// two elements is not greater than Ximport java.io.*;import java.lang.*;import java.util.*;class GFG{   // Function that prints the longest sub-array  // where the absolute difference between any  // two element is not greater than X  static void longestSubarray(int A[], int N, int X)  {     // Initialize a variable to store    // length of longest sub-array    int maxLen = 0;     // Initialize a variable to store the    // beginning of the longest sub-array    int beginning = 0;     // Initialize a map to store the maximum    // and the minimum elements for a given window    TreeMap<Integer, Integer> window = new TreeMap<>();     // Initialize the window    int start = 0, end = 0;     // Loop through the array    for (; end < N; end++)    {       // Increment the count of that      // element in the window      window.put(A[end],                 window.getOrDefault(A[end], 0) + 1);       // Find the maximum and minimum element      // in the current window      int minimum = window.firstKey();      int maximum = window.lastKey();       // If the difference is not      // greater than X      if (maximum - minimum <= X)      {         // Update the length of the longest        // sub-array and store the beginning        // of the sub-array        if (maxLen < end - start + 1) {          maxLen = end - start + 1;          beginning = start;        }      }       // Decrease the size of the window      else {        while (start < end)        {           // Remove the element at start          window.put(A[start],                     window.get(A[start]) - 1);           // Remove the element from the window          // if its count is zero          if (window.get(A[start]) == 0) {             window.remove(A[start]);          }           // Increment the start of the window          start++;           // Find the maximum and minimum element          // in the current window          minimum = window.firstKey();          maximum = window.lastKey();           // Stop decreasing the size of window          // when difference is not greater          if (maximum - minimum <= X)            break;        }      }    }     // Print the longest sub-array    for (int i = beginning; i < beginning + maxLen; i++)      System.out.print(A[i] + " ");  }   // Driver Code  public static void main(String[] args)  {     // Given array    int arr[] = { 15, 10, 1, 2, 4, 7, 2 }, X = 5;     // store the size of the array    int n = arr.length;     // Function call    longestSubarray(arr, n, X);  }} // This code is contributed by Kingash.
# Python3 program to find the longest sub-array# where the absolute difference between any# two elements is not greater than X # Function that prints the longest sub-array# where the absolute difference between any# two element is not greater than Xdef longestSubarray(A, N, X):         # Initialize a variable to store    # length of longest sub-array    maxLen = 0     # Initialize a variable to store the    # beginning of the longest sub-array    beginning = 0     # Initialize a map to store the maximum    # and the minimum elements for a given window    window = {}     # Initialize the window    start = 0     # Loop through the array    for end in range(N):         # Increment the count of that        # element in the window        if A[end] in window:            window[A[end]] += 1        else:            window[A[end]] = 1         # Find the maximum and minimum element        # in the current window        minimum = min(list(window.keys()))        maximum = max(list(window.keys()))         # If the difference is not        # greater than X        if maximum - minimum <= X:                         # Update the length of the longest            # sub-array and store the beginning            # of the sub-array            if maxLen < end - start + 1:                maxLen = end - start + 1                beginning = start         # Decrease the size of the window        else:            while start < end:                 # Remove the element at start                window[A[start]] -= 1                 # Remove the element from the window                # if its count is zero                if window[A[start]] == 0:                    window.pop(A[start])                 # Increment the start of the window                start += 1                 # Find the maximum and minimum element                # in the current window                minimum = min(list(window.keys()))                maximum = max(list(window.keys()))                 # Stop decreasing the size of window                # when difference is not greater                if maximum - minimum <= X:                    break                         # Print the longest sub-array    for i in range(beginning, beginning + maxLen):        print(A[i], end = ' ') # Driver Codearr = [15, 10, 1, 2, 4, 7, 2]X = 5n = len(arr)longestSubarray(arr, n, X) # This code is contributed by Shivam Singh
<script> // JavaScript program to find the longest sub-array// where the absolute difference between any// two elements is not greater than X // Function that prints the longest sub-array// where the absolute difference between any// two element is not greater than Xfunction longestSubarray(A, N, X){         // Initialize a variable to store    // length of longest sub-array    let maxLen = 0     // Initialize a variable to store the    // beginning of the longest sub-array    let beginning = 0     // Initialize a map to store the maximum    // and the minimum elements for a given window    let window = new Map()     // Initialize the window    let start = 0     // Loop through the array    for(let end=0;end<N;end++){         // Increment the count of that        // element in the window        if(window.has(A[end]))            window.set(A[end],window.get(A[end]) + 1)        else            window.set(A[end] , 1)         // Find the maximum and minimum element        // in the current window        let minimum = Math.min(...window.keys())        let maximum = Math.max(...window.keys())         // If the difference is not        // greater than X        if(maximum - minimum <= X){                         // Update the length of the longest            // sub-array and store the beginning            // of the sub-array            if(maxLen < end - start + 1){                maxLen = end - start + 1                beginning = start            }        }         // Decrease the size of the window        else{            while(start < end){                 // Remove the element at start                window.set(A[start],window.get(A[start]) - 1)                 // Remove the element from the window                // if its count is zero                if(window.get(A[start]) == 0)                    window.delete(A[start])                 // Increment the start of the window                start += 1                 // Find the maximum and minimum element                // in the current window                minimum = Math.min(...window.keys())                maximum = Math.max(...window.keys())                 // Stop decreasing the size of window                // when difference is not greater                if(maximum - minimum <= X)                    break            }        }    }                         // Print the longest sub-array    for(let i=beginning;i<beginning+maxLen;i++){        document.write(A[i],' ')    }} // Driver Codelet arr = [15, 10, 1, 2, 4, 7, 2]let X = 5let n = arr.lengthlongestSubarray(arr, n, X) // This code is contributed by Shinjanpatra </script>
2 4 7 2
 
Time Complexity: O(N * log(N)) 
SHIVAMSINGH67
Kingash
sweetyty
shinjanpatra
vinayedula
sliding-window
subarray
Arrays
Competitive Programming
Mathematical
sliding-window
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Arrays
Introduction to Data Structures
Search an element in a sorted and rotated array
Find Second largest element in an array
Count Inversions in an array | Set 1 (Using Merge Sort)
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Modulo 10^9+7 (1000000007)
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming