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++ program to find the minimum // and maximum length subarray such // that difference between adjacent // elements is atmost K #include <iostream> using namespace std;
// Function to find the maximum and // minimum length subarray void findMaxMinSubArray( int arr[],
int K, int n)
{ // Initialise minimum and maximum
// size of subarray in worst case
int min = n;
int max = 0;
// Left will scan the size of
// possible subarray in left
// of selected element
int left;
// Right will scan the size of
// possible subarray in right
// of selected element
int right;
// Temp will store size of group
// associated with every element
int tmp;
// Loop to find size of subarray
// for every element of array
for ( int i = 0; i < n; i++) {
tmp = 1;
left = i;
// Left will move in left direction
// and compare difference between
// itself and element left to it
while (left - 1 >= 0
&& abs (arr[left] - arr[left - 1])
<= K) {
left--;
tmp++;
}
// right will move in right direction
// and compare difference between
// itself and element right to it
right = i;
while (right + 1 <= n - 1
&& abs (arr[right] - arr[right + 1])
<= K) {
right++;
tmp++;
}
// if subarray of much lesser or much
// greater is found than yet
// known then update
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
// Print minimum and maximum
// possible size of subarray
cout << min << ", "
<< max << endl;
} // Driver Code int main()
{ int arr[] = { 1, 2, 5, 6, 7 };
int K = 2;
int n = sizeof (arr) / sizeof (arr[0]);
// function call to find maximum
// and minimum possible sub array
findMaxMinSubArray(arr, K, n);
return 0;
} |
// Java program to find the minimum // and maximum length subarray such // that difference between adjacent // elements is atmost K import java.io.*;
import java.util.*;
class GFG {
// Function to find the maximum and // minimum length subarray static void findMaxMinSubArray( int arr[],
int K, int n)
{ // Initialise minimum and maximum
// size of subarray in worst case
int min = n;
int max = 0 ;
// Left will scan the size of
// possible subarray in left
// of selected element
int left;
// Right will scan the size of
// possible subarray in right
// of selected element
int right;
// Temp will store size of group
// associated with every element
int tmp;
// Loop to find size of subarray
// for every element of array
for ( int i = 0 ; i < n; i++)
{
tmp = 1 ;
left = i;
// Left will move in left direction
// and compare difference between
// itself and element left to it
while (left - 1 >= 0 &&
Math.abs(arr[left] -
arr[left - 1 ]) <= K)
{
left--;
tmp++;
}
// Right will move in right direction
// and compare difference between
// itself and element right to it
right = i;
while (right + 1 <= n - 1 &&
Math.abs(arr[right] -
arr[right + 1 ]) <= K)
{
right++;
tmp++;
}
// If subarray of much lesser or much
// greater is found than yet
// known then update
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
// Print minimum and maximum
// possible size of subarray
System.out.print(min);
System.out.print( ", " );
System.out.print(max);
} // Driver code public static void main(String[] args)
{ int arr[] = { 1 , 2 , 5 , 6 , 7 };
int K = 2 ;
int n = arr.length;
// Function call to find maximum
// and minimum possible sub array
findMaxMinSubArray(arr, K, n);
} } // This code is contributed by coder001 |
# Python3 program to find the minimum # and maximum length subarray such # that difference between adjacent # elements is atmost K # Function to find the maximum and # minimum length subarray def findMaxMinSubArray(arr, K, n):
# Initialise minimum and maximum
# size of subarray in worst case
min = n
max = 0
# Left will scan the size of
# possible subarray in left
# of selected element
left = 0
# Right will scan the size of
# possible subarray in right
# of selected element
right = n
# Temp will store size of group
# associated with every element
tmp = 0
# Loop to find size of subarray
# for every element of array
for i in range ( 0 , n):
tmp = 1
left = i
# Left will move in left direction
# and compare difference between
# itself and element left to it
while (left - 1 > = 0 and
abs (arr[left] -
arr[left - 1 ]) < = K):
left = left - 1
tmp = tmp + 1
# Right will move in right direction
# and compare difference between
# itself and element right to it
right = i
while (right + 1 < = n - 1 and
abs (arr[right] -
arr[right + 1 ]) < = K):
right = right + 1
tmp = tmp + 1
# If subarray of much lesser or much
# greater is found than yet
# known then update
if ( min > tmp):
min = tmp
if ( max < tmp):
max = tmp
# Print minimum and maximum
# possible size of subarray
print ( min , end = ', ' )
print ( max , end = '\n' )
# Driver Code arr = [ 1 , 2 , 5 , 6 , 7 ]
K = 2
n = len (arr)
# Function call to find maximum # and minimum possible sub array findMaxMinSubArray(arr, K, n) # This code is contributed by Pratik |
// C# program to find the minimum // and maximum length subarray such // that difference between adjacent // elements is atmost K using System;
class GFG{
// Function to find the maximum and // minimum length subarray static void findMaxMinSubArray( int [] arr,
int K, int n)
{ // Initialise minimum and maximum
// size of subarray in worst case
int min = n;
int max = 0;
// Left will scan the size of
// possible subarray in left
// of selected element
int left;
// Right will scan the size of
// possible subarray in right
// of selected element
int right;
// Temp will store size of group
// associated with every element
int tmp;
// Loop to find size of subarray
// for every element of array
for ( int i = 0; i < n; i++)
{
tmp = 1;
left = i;
// Left will move in left direction
// and compare difference between
// itself and element left to it
while (left - 1 >= 0 &&
Math.Abs(arr[left] -
arr[left - 1]) <= K)
{
left--;
tmp++;
}
// Right will move in right direction
// and compare difference between
// itself and element right to it
right = i;
while (right + 1 <= n - 1 &&
Math.Abs(arr[right] -
arr[right + 1]) <= K)
{
right++;
tmp++;
}
// If subarray of much lesser or much
// greater is found than yet
// known then update
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
// Print minimum and maximum
// possible size of subarray
Console.Write(min);
Console.Write( ", " );
Console.Write(max);
} // Driver code public static void Main()
{ int [] arr = { 1, 2, 5, 6, 7 };
int K = 2;
int n = arr.Length;
// Function call to find maximum
// and minimum possible sub array
findMaxMinSubArray(arr, K, n);
} } // This code is contributed by AbhiThakur |
<script> // Java Script program to find the minimum // and maximum length subarray such // that difference between adjacent // elements is atmost K // Function to find the maximum and // minimum length subarray function findMaxMinSubArray(arr, k, n)
{ // Initialise minimum and maximum
// size of subarray in worst case
let min = n;
let max = 0;
// Left will scan the size of
// possible subarray in left
// of selected element
let left;
// Right will scan the size of
// possible subarray in right
// of selected element
let right;
// Temp will store size of group
// associated with every element
let tmp;
// Loop to find size of subarray
// for every element of array
for (let i = 0; i < n; i++)
{
tmp = 1;
left = i;
// Left will move in left direction
// and compare difference between
// itself and element left to it
while (left - 1 >= 0 &&
Math.abs(arr[left] -
arr[left - 1]) <= K)
{
left--;
tmp++;
}
// Right will move in right direction
// and compare difference between
// itself and element right to it
right = i;
while (right + 1 <= n - 1 &&
Math.abs(arr[right] -
arr[right + 1]) <= K)
{
right++;
tmp++;
}
// If subarray of much lesser or much
// greater is found than yet
// known then update
if (min > tmp)
min = tmp;
if (max < tmp)
max = tmp;
}
// Print minimum and maximum
// possible size of subarray
document.write(min);
document.write( ", " );
document.write(max);
} // Driver code let arr = [1, 2, 5, 6, 7 ];
let K = 2;
let n = arr.length;
// Function call to find maximum
// and minimum possible sub array
findMaxMinSubArray(arr, K, n);
// This code is contributed by sravan </script> |
2, 3