Maximum length subarray with difference between adjacent elements as either 0 or 1
Given an array of n integers. The task is to find the maximum length of the sub-array such that absolute difference between all the consecutive elements of the sub-array is either 0 or 1.
Examples:
Input: arr[] = {2, 5, 6, 3, 7, 6, 5, 8}
Output: 3
{5, 6} and {7, 6, 5} are the only valid sub-arrays.
Input: arr[] = {-2, -1, 5, -1, 4, 0, 3}
Output: 2
Approach: Starting from the first element of the array, find the first valid sub-array and store it’s 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 maximum 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 l = n;
int i = 0, maxlen = 0;
while (i < l)
{
int j = i;
while (i+1 < l &&
( abs (arr[i] - arr[i + 1]) == 1 ||
abs (arr[i] - arr[i + 1]) == 0))
{
i++;
}
int currLen = i - j + 1;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
return maxlen;
}
int main()
{
int arr[] = { 2, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getMaxLength(arr, n);
}
|
Java
public class GFG {
public static int getMaxLength( int arr[])
{
int l = arr.length;
int i = 0 , maxlen = 0 ;
while (i < l) {
int j = i;
while (i + 1 < l
&& (Math.abs(arr[i] - arr[i + 1 ]) == 1
|| Math.abs(arr[i] - arr[i + 1 ]) == 0 )) {
i++;
}
int currLen = i - j + 1 ;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
maxlen = (maxlen == 1 ) ? 0 : maxlen;
return maxlen;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 };
System.out.print(getMaxLength(arr));
}
}
|
Python3
def getMaxLength(arr, n) :
l = n;
i = 0 ; maxlen = 0 ;
while (i < l) :
j = i;
while (i + 1 < l and
( abs (arr[i] - arr[i + 1 ]) = = 1 or
abs (arr[i] - arr[i + 1 ]) = = 0 )) :
i + = 1 ;
currLen = i - j + 1 ;
if (maxlen < currLen) :
maxlen = currLen;
if (j = = i) :
i + = 1 ;
return maxlen;
if __name__ = = "__main__" :
arr = [ 2 , 4 ];
n = len (arr)
print (getMaxLength(arr, n));
|
C#
using System;
class GFG
{
public static int getMaxLength( int []arr)
{
int l = arr.Length;
int i = 0, maxlen = 0;
while (i < l)
{
int j = i;
while (i + 1 < l &&
(Math.Abs(arr[i] - arr[i + 1]) == 1 ||
Math.Abs(arr[i] - arr[i + 1]) == 0))
{
i++;
}
int currLen = i - j + 1;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
maxlen = (maxlen == 1) ? 0 : maxlen;
return maxlen;
}
public static void Main(String []args)
{
int []arr = { 2, 4 };
Console.Write(getMaxLength(arr));
}
}
|
PHP
<?php
function getMaxLength( $arr , $n )
{
$l = $n ;
$i = 0;
$maxlen = 0;
while ( $i < $l )
{
$j = $i ;
while ( $i + 1 < $l &&
( abs ( $arr [ $i ] - $arr [ $i + 1]) == 1 ||
abs ( $arr [ $i ] - $arr [ $i + 1]) == 0))
{
$i ++;
}
$currLen = $i - $j + 1;
if ( $maxlen < $currLen )
$maxlen = $currLen ;
if ( $j == $i )
$i ++;
}
return $maxlen ;
}
$arr = array (2, 4 );
$n = sizeof( $arr );
echo getMaxLength( $arr , $n )
?>
|
Javascript
<script>
function getMaxLength(arr)
{
let l = arr.length;
let i = 0, maxlen = 0;
while (i < l) {
let j = i;
while (i + 1 < l
&& (Math.abs(arr[i] - arr[i + 1]) == 1
|| Math.abs(arr[i] - arr[i + 1]) == 0)) {
i++;
}
let currLen = i - j + 1;
if (maxlen < currLen)
maxlen = currLen;
if (j == i)
i++;
}
return maxlen;
}
let arr = [2, 4 ];
document.write(getMaxLength(arr));
</script>
|
Time Complexity : O(n) ,where n is size of given array.
Space Complexity : O(1) ,as we are not using any extra space.
Last Updated :
23 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...