Min and max length subarray having adjacent element difference atmost K
Last Updated :
25 Mar, 2022
Given an array arr[] and an integer K, the task is to find the maximum and minimum length subarray such that the difference between adjacent elements is at most K.
Examples:
Input: arr[] = {2, 4, 6}, K = 2
Output: 3, 3
Explanation:
Minimum and Maximum length subarray such that difference
between adjacent elements is at most is 3, which is {2, 4, 2}
Input: arr[] = {2, 3, 6, 7, 8}, K = 2
Output: 2, 3
Explanation:
Minimum length Subarray(2) => {2, 3}
Maximum length Subarray(3) => {6, 7, 8}
Approach: The idea is traverse the array, to start from each element and move to its right and left direction until the difference between the adjacent elements is less than K. Finally, update the maximum and minimum length subarray with the current length as defined below –
if (current_length maximum_length)
maximum_length = current_length
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void findMaxMinSubArray( int arr[],
int K, int n)
{
int min = n;
int max = 0;
int left;
int right;
int tmp;
for ( int i = 0; i < n; i++) {
tmp = 1;
left = i;
while (left - 1 >= 0
&& abs (arr[left] - arr[left - 1])
<= K) {
left--;
tmp++;
}
right = i;
while (right + 1 <= n - 1
&& abs (arr[right] - arr[right + 1])
<= K) {
right++;
tmp++;
}
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
cout << min << ", "
<< max << endl;
}
int main()
{
int arr[] = { 1, 2, 5, 6, 7 };
int K = 2;
int n = sizeof (arr) / sizeof (arr[0]);
findMaxMinSubArray(arr, K, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void findMaxMinSubArray( int arr[],
int K, int n)
{
int min = n;
int max = 0 ;
int left;
int right;
int tmp;
for ( int i = 0 ; i < n; i++)
{
tmp = 1 ;
left = i;
while (left - 1 >= 0 &&
Math.abs(arr[left] -
arr[left - 1 ]) <= K)
{
left--;
tmp++;
}
right = i;
while (right + 1 <= n - 1 &&
Math.abs(arr[right] -
arr[right + 1 ]) <= K)
{
right++;
tmp++;
}
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
System.out.print(min);
System.out.print( ", " );
System.out.print(max);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 5 , 6 , 7 };
int K = 2 ;
int n = arr.length;
findMaxMinSubArray(arr, K, n);
}
}
|
Python3
def findMaxMinSubArray(arr, K, n):
min = n
max = 0
left = 0
right = n
tmp = 0
for i in range ( 0 , n):
tmp = 1
left = i
while (left - 1 > = 0 and
abs (arr[left] -
arr[left - 1 ]) < = K):
left = left - 1
tmp = tmp + 1
right = i
while (right + 1 < = n - 1 and
abs (arr[right] -
arr[right + 1 ]) < = K):
right = right + 1
tmp = tmp + 1
if ( min > tmp):
min = tmp
if ( max < tmp):
max = tmp
print ( min , end = ', ' )
print ( max , end = '\n' )
arr = [ 1 , 2 , 5 , 6 , 7 ]
K = 2
n = len (arr)
findMaxMinSubArray(arr, K, n)
|
C#
using System;
class GFG{
static void findMaxMinSubArray( int [] arr,
int K, int n)
{
int min = n;
int max = 0;
int left;
int right;
int tmp;
for ( int i = 0; i < n; i++)
{
tmp = 1;
left = i;
while (left - 1 >= 0 &&
Math.Abs(arr[left] -
arr[left - 1]) <= K)
{
left--;
tmp++;
}
right = i;
while (right + 1 <= n - 1 &&
Math.Abs(arr[right] -
arr[right + 1]) <= K)
{
right++;
tmp++;
}
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
Console.Write(min);
Console.Write( ", " );
Console.Write(max);
}
public static void Main()
{
int [] arr = { 1, 2, 5, 6, 7 };
int K = 2;
int n = arr.Length;
findMaxMinSubArray(arr, K, n);
}
}
|
Javascript
<script>
function findMaxMinSubArray(arr, k, n)
{
let min = n;
let max = 0;
let left;
let right;
let tmp;
for (let i = 0; i < n; i++)
{
tmp = 1;
left = i;
while (left - 1 >= 0 &&
Math.abs(arr[left] -
arr[left - 1]) <= K)
{
left--;
tmp++;
}
right = i;
while (right + 1 <= n - 1 &&
Math.abs(arr[right] -
arr[right + 1]) <= K)
{
right++;
tmp++;
}
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
document.write(min);
document.write( ", " );
document.write(max);
}
let arr = [1, 2, 5, 6, 7 ];
let K = 2;
let n = arr.length;
findMaxMinSubArray(arr, K, n);
</script>
|
Share your thoughts in the comments
Please Login to comment...