Longest subarray such that the difference of max and min is at-most one
Given an array of n numbers where the difference between each number and the previous one doesn’t exceed one. Find the longest contiguous subarray such that the difference between the maximum and minimum number in the range doesn’t exceed one.
Examples:
Input : {3, 3, 4, 4, 5, 6}
Output : 4
The longest subarray here is {3, 3, 4, 4}
Input : {7, 7, 7}
Output : 3
The longest subarray here is {7, 7, 7}
Input : {9, 8, 8, 9, 9, 10}
Output : 5
The longest subarray here is {9, 8, 8, 9, 9}
If the difference between the maximum and the minimum numbers in the sequence doesn’t exceed one then the sequence consisted of only one or two consecutive numbers. The idea is to traverse the array and keep track of current element and previous element in current subarray. If we find an element which is not same as current or previous, we update current and previous. We also update result if required.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int longestSubarray( int input[],
int length)
{
int prev = -1;
int current, next;
int prevCount = 0, currentCount = 1;
int longest = 1;
current = input[0];
for ( int i = 1; i < length; i++)
{
next = input[i];
if (next == current)
{
currentCount++;
}
else if (next == prev)
{
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1;
}
else
{
longest = max(longest,
currentCount + prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1;
}
}
return max(longest,
currentCount + prevCount);
}
int main()
{
int input[] = { 5, 5, 6, 7, 6 };
int n = sizeof (input) / sizeof ( int );
cout << longestSubarray(input, n);
return 0;
}
|
Java
public class Demo {
public static int longestSubarray( int [] input)
{
int prev = - 1 ;
int current, next;
int prevCount = 0 , currentCount = 1 ;
int longest = 1 ;
current = input[ 0 ];
for ( int i = 1 ; i < input.length; i++) {
next = input[i];
if (next == current) {
currentCount++;
}
else if (next == prev) {
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1 ;
}
else {
longest = Math.max(longest, currentCount + prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1 ;
}
}
return Math.max(longest, currentCount + prevCount);
}
public static void main(String[] args)
{
int [] input = { 5 , 5 , 6 , 7 , 6 };
System.out.println(longestSubarray(input));
}
}
|
Python 3
def longestSubarray( input , length):
prev = - 1
prevCount = 0
currentCount = 1
longest = 1
current = input [ 0 ]
for i in range ( 1 , length):
next = input [i]
if next = = current :
currentCount + = 1
elif next = = prev :
prevCount + = currentCount
prev = current
current = next
currentCount = 1
else :
longest = max (longest,
currentCount +
prevCount)
prev = current
prevCount = currentCount
current = next
currentCount = 1
return max (longest,
currentCount + prevCount)
if __name__ = = "__main__" :
input = [ 5 , 5 , 6 , 7 , 6 ]
n = len ( input )
print (longestSubarray( input , n))
|
C#
using System;
class GFG
{
public static int longestSubarray( int [] input)
{
int prev = -1;
int current, next;
int prevCount = 0,
currentCount = 1;
int longest = 1;
current = input[0];
for ( int i = 1;
i < input.Length; i++)
{
next = input[i];
if (next == current)
{
currentCount++;
}
else if (next == prev)
{
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1;
}
else
{
longest = Math.Max(longest,
currentCount +
prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1;
}
}
return Math.Max(longest,
currentCount + prevCount);
}
public static void Main(String[] args)
{
int [] input = {5, 5, 6, 7, 6};
Console.WriteLine(longestSubarray(input));
}
}
|
PHP
<?php
function longestSubarray( $input , $length )
{
$prev = -1;
$prevCount = 0;
$currentCount = 1;
$longest = 1;
$current = $input [0];
for ( $i = 1; $i < $length ; $i ++)
{
$next = $input [ $i ];
if ( $next == $current )
{
$currentCount ++;
}
else if ( $next == $prev )
{
$prevCount += $currentCount ;
$prev = $current ;
$current = $next ;
$currentCount = 1;
}
else
{
$longest = max( $longest ,
$currentCount +
$prevCount );
$prev = $current ;
$prevCount = $currentCount ;
$current = $next ;
$currentCount = 1;
}
}
return max( $longest ,
$currentCount + $prevCount );
}
$input = array ( 5, 5, 6, 7, 6 );
echo (longestSubarray( $input , count ( $input )));
?>
|
Javascript
<script>
function longestSubarray(input)
{
let prev = -1;
let current, next;
let prevCount = 0, currentCount = 1;
let longest = 1;
current = input[0];
for (let i = 1; i < input.length; i++) {
next = input[i];
if (next == current) {
currentCount++;
}
else if (next == prev) {
prevCount += currentCount;
prev = current;
current = next;
currentCount = 1;
}
else {
longest = Math.max(longest,
currentCount + prevCount);
prev = current;
prevCount = currentCount;
current = next;
currentCount = 1;
}
}
return Math.max(longest,
currentCount + prevCount);
}
let input=[5, 5, 6, 7, 6];
document.write(longestSubarray(input));
</script>
|
Time Complexity: O(n) where n is the length of the input array.
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...