Longest subarray with difference exactly K between any two distinct values

30 Sep, 2021
Given an array arr[] of length N and an integer K, the task is to find the longest subarray with difference between any two distinct values equal to K. Print the length of the longest subarray obtained. Otherwise, if no such subarray is obtained, print -1.
Examples: 
Input: arr[] = {0, 0, 1, 1, 3, 3, 3}, K = 1 Output: 4 Explanation: The subarray {0, 0, 1, 1} is the only subarray having difference between any two distinct values equal to K( = 1). Hence, length is equal to 4.
Input: arr[] = {5, 7, 1, 1, 2, 4, 4, 4, 5, 5, 4, 5, 8, 9}, K = 1 Output: 7 Explanation: Subarrays {1, 1, 2}, {4, 4, 4, 5, 5, 4, 5} and {8, 9} are the only subarray having difference between any two distinct values equal to K( = 1). The longest subarray among them is {4, 4, 4, 5, 5, 4, 5}. Hence, the length is 7. 
Naive Approach:
A simple solution is to consider all subarrays one by one, and find subarrays which contains only two distinct values and the difference between those two values is K. Keep updating the maximum length of subarray obtained.
Finally print the maximum length obtained.
Time Complexity: O(N3) Auxiliary Space: O(N)
Efficient Approach: It can be observed that for any subarray to consist of elements with the difference between any two elements to be exactly K, the subarray must consist of only two distinct values. Hence, the above approach can be further optimized by using set to find longest sub-array having only two distinct values with a difference K. Follow the steps below to solve the problem: 
Start the first subarray from starting index of the array.
Insert that element into the set. Proceed to the next element and check if this element is the same as the previous or has an absolute difference of K.
If so, insert that element into the set and continue increasing the length of the subarray. Once, a third distinct element is found, compare the length of the current subarray with the maximum length of the subarray and update accordingly.
Update the new element obtained into the set and proceed to repeat the above steps.
Once, the entire array is traversed, print the maximum length obtained.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to find the// longest subarray consisting of// only two values with difference K#include <bits/stdc++.h>using namespace std; // Function to return the length// of the longest sub-arrayint longestSubarray(int arr[], int n,                    int k){    int i, j, Max = 1;     // Initialize set    set<int> s;     for (i = 0; i < n - 1; i++) {        // Store 1st element of        // sub-array into set        s.insert(arr[i]);         for (j = i + 1; j < n; j++) {            // Check absolute difference            // between two elements             if (abs(arr[i] - arr[j]) == 0                || abs(arr[i] - arr[j]) == k) {                 // If the new element is not                // present in the set                if (!s.count(arr[j])) {                     // If the set contains                    // two elements                    if (s.size() == 2)                        break;                     // Otherwise                    else                        s.insert(arr[j]);                }            }            else                break;        }         if (s.size() == 2) {             // Update the maximum            // length            Max = max(Max, j - i);             // Remove the set            // elements            s.clear();        }        else            s.clear();    }     return Max;} // Driver Codeint main(){    int arr[] = { 1, 0, 2, 2, 5, 5, 5 };     int N = sizeof(arr)            / sizeof(arr[0]);    int K = 1;     int length = longestSubarray(        arr, N, K);     if (length == 1)        cout << -1;    else        cout << length;     return 0;}
// Java implementation to find the// longest subarray consisting of// only two values with difference Kimport java.util.*; class GFG{ // Function to return the length// of the longest sub-arraystatic int longestSubarray(int arr[], int n,                        int k){    int i, j, Max = 1;     // Initialize set    HashSet<Integer> s = new HashSet<Integer>();     for(i = 0; i < n - 1; i++)    {                 // Store 1st element of        // sub-array into set        s.add(arr[i]);         for(j = i + 1; j < n; j++)        {                         // Check absolute difference            // between two elements            if (Math.abs(arr[i] - arr[j]) == 0 ||                Math.abs(arr[i] - arr[j]) == k)            {                                 // If the new element is not                // present in the set                if (!s.contains(arr[j]))                {                                         // If the set contains                    // two elements                    if (s.size() == 2)                        break;                     // Otherwise                    else                        s.add(arr[j]);                }            }            else                break;        }        if (s.size() == 2)        {                         // Update the maximum            // length            Max = Math.max(Max, j - i);             // Remove the set            // elements            s.clear();        }        else            s.clear();    }    return Max;} // Driver Codepublic static void main(String[] args){    int arr[] = { 1, 0, 2, 2, 5, 5, 5 };     int N = arr.length;    int K = 1;    int length = longestSubarray(arr, N, K);     if (length == 1)        System.out.print(-1);    else        System.out.print(length);}} // This code is contributed by Princi Singh
# Python3 implementation to find the # longest subarray consisting of # only two values  with difference K # Function to return the length # of the longest sub-arraydef longestSubarray (arr, n, k):     Max = 1     # Initialize set    s = set()     for i in range(n - 1):         # Store 1st element of        # sub-array into set        s.add(arr[i])         for j in range(i + 1, n):                         # Check absolute difference            # between two elements            if (abs(arr[i] - arr[j]) == 0 or                abs(arr[i] - arr[j]) == k):                 # If the new element is not                # present in the set                if (not arr[j] in s):                     # If the set contains                    # two elements                    if (len(s) == 2):                        break                     # Otherwise                    else:                        s.add(arr[j])                                     else:                break         if (len(s) == 2):             # Update the maximum length            Max = max(Max, j - i)             # Remove the set elements            s.clear()         else:            s.clear()     return Max # Driver Codeif __name__ == '__main__':         arr = [ 1, 0, 2, 2, 5, 5, 5 ]     N = len(arr)    K = 1     length = longestSubarray(arr, N, K)     if (length == 1):        print("-1")    else:        print(length) # This code is contributed by himanshu77
// C# implementation to find the// longest subarray consisting of// only two values with difference Kusing System;using System.Collections.Generic; class GFG{ // Function to return the length// of the longest sub-arraystatic int longestSubarray(int []arr, int n,                                      int k){    int i, j, Max = 1;     // Initialize set    HashSet<int> s = new HashSet<int>();     for(i = 0; i < n - 1; i++)    {                 // Store 1st element of        // sub-array into set        s.Add(arr[i]);         for(j = i + 1; j < n; j++)        {                         // Check absolute difference            // between two elements            if (Math.Abs(arr[i] - arr[j]) == 0 ||                Math.Abs(arr[i] - arr[j]) == k)            {                                 // If the new element is not                // present in the set                if (!s.Contains(arr[j]))                {                                         // If the set contains                    // two elements                    if (s.Count == 2)                        break;                     // Otherwise                    else                        s.Add(arr[j]);                }            }            else                break;        }        if (s.Count == 2)        {                         // Update the maximum            // length            Max = Math.Max(Max, j - i);             // Remove the set            // elements            s.Clear();        }        else            s.Clear();    }    return Max;} // Driver Codepublic static void Main(String[] args){    int []arr = { 1, 0, 2, 2, 5, 5, 5 };     int N = arr.Length;    int K = 1;    int length = longestSubarray(arr, N, K);     if (length == 1)        Console.Write(-1);    else        Console.Write(length);}} // This code is contributed by Princi Singh
<script>    // Javascript implementation to find the    // longest subarray consisting of    // only two values with difference K         // Function to return the length    // of the longest sub-array    function longestSubarray(arr, n, k)    {        let i, j, Max = 1;         // Initialize set        let s = new Set();         for (i = 0; i < n - 1; i++) {            // Store 1st element of            // sub-array into set            s.add(arr[i]);             for (j = i + 1; j < n; j++) {                // Check absolute difference                // between two elements                 if (Math.abs(arr[i] - arr[j]) == 0                    || Math.abs(arr[i] - arr[j]) == k) {                     // If the new element is not                    // present in the set                    if (!s.has(arr[j])) {                         // If the set contains                        // two elements                        if (s.size == 2)                            break;                         // Otherwise                        else                            s.add(arr[j]);                    }                }                else                    break;            }             if (s.size == 2) {                 // Update the maximum                // length                Max = Math.max(Max, j - i);                 // Remove the set                // elements                s.clear;            }            else                s.clear;        }         return Max;    }         let arr = [ 1, 0, 2, 2, 5, 5, 5 ];      let N = arr.length;    let K = 1;      let length = longestSubarray(arr, N, K);      if (length == 1)        document.write(-1);    else        document.write(length); // This code is contributed by decode2207.</script>
2
 
Time Complexity: O(N2 * logN) Auxiliary Space: O(N)  
princi singh
himanshu77
anikaseth98
decode2207
cpp-set
subarray
two-pointer-algorithm
Arrays
Competitive Programming
Searching
two-pointer-algorithm
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.