Length of Longest subarray such that difference between adjacent elements is K
Last Updated :
24 Jul, 2022
Given an array arr[] of size N, and integer K. The task is to find the length of the longest subarray with the difference between adjacent elements as K.
Examples:
Input: arr[] = { 5, 5, 5, 10, 8, 6, 12, 13 }, K =1
Output: 2
Explanation: Only one subarray which have difference between adjacents as 1 is {12, 13}.
Input: arr[] = {4, 6, 8, 9, 8, 12, 14, 17, 15}, K = 2
Output: 3
Explanation: There are three such subarrays {4, 6, 8}, {12, 14} and {17, 15}.
{4, 6, 8} has the highest length.
Input: arr[] = {2, 2, 4, 6}, K = 1
Output: 1
Explanation: No subarray of length more than satisfies this criteria.
Approach: Starting from the first element of the array, find the first valid sub-array and store its length then starting from the next element (the first element that wasn’t included in the first sub-array), find another valid sub-array. Repeat the process until all the valid sub-arrays have been found then print the length of the longest sub-array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaxLength( int arr[], int N, int K)
{
int l = N;
int i = 0, maxlen = 0;
while (i < l) {
int j = i;
while (i + 1 < l
&& ( abs (arr[i] -
arr[i + 1]) == K)) {
i++;
}
int currLen = i - j + 1;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
return maxlen;
}
int main()
{
int arr[] = { 2, 2, 4, 6 };
int K = 1;
int N = sizeof (arr) / sizeof (arr[0]);
cout << getMaxLength(arr, N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int getMaxLength( int arr[], int N, int K)
{
int l = N;
int i = 0 , maxlen = 0 ;
while (i < l) {
int j = i;
while (i + 1 < l
&& (Math.abs(arr[i] -
arr[i + 1 ]) == K)) {
i++;
}
int currLen = i - j + 1 ;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
return maxlen;
}
public static void main(String args[])
{
int arr[] = { 2 , 2 , 4 , 6 };
int K = 1 ;
int N = arr.length;
System.out.print(getMaxLength(arr, N, K));
}
}
|
Python3
def getMaxLength (arr, N, K):
l = N;
i = 0
maxlen = 0 ;
while (i < l):
j = i;
while (i + 1 < l and ( abs (arr[i] - arr[i + 1 ]) = = K)):
i + = 1
currLen = i - j + 1 ;
if (maxlen < currLen):
maxlen = currLen;
if (j = = i):
i + = 1
return maxlen;
arr = [ 2 , 2 , 4 , 6 ];
K = 1 ;
N = len (arr)
print (getMaxLength(arr, N, K));
|
C#
using System;
class GFG
{
static int getMaxLength( int []arr, int N, int K)
{
int l = N;
int i = 0, maxlen = 0;
while (i < l) {
int j = i;
while (i + 1 < l
&& (Math.Abs(arr[i] -
arr[i + 1]) == K)) {
i++;
}
int currLen = i - j + 1;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
return maxlen;
}
public static void Main()
{
int []arr = { 2, 2, 4, 6 };
int K = 1;
int N = arr.Length;
Console.Write(getMaxLength(arr, N, K));
}
}
|
Javascript
<script>
const getMaxLength = (arr, N, K) => {
let l = N;
let i = 0, maxlen = 0;
while (i < l) {
let j = i;
while (i + 1 < l
&& (Math.abs(arr[i] -
arr[i + 1]) == K)) {
i++;
}
let currLen = i - j + 1;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
return maxlen;
}
let arr = [2, 2, 4, 6];
let K = 1;
let N = arr.length;
document.write(getMaxLength(arr, N, K));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...