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++
// C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return the maximum length // of the sub-array such that the // absolute difference between every two // consecutive elements is either 1 or 0 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++; } // Length of the valid sub-array currently // under consideration int currLen = i - j + 1; // Update the maximum length if (maxlen < currLen) maxlen = currLen; if (j == i) i++; } // Any valid sub-array cannot be of length 1 //maxlen = (maxlen == 1) ? 0 : maxlen; // Return the maximum possible length return maxlen; } // Driver code int main() { int arr[] = { 2, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << getMaxLength(arr, n); } // This code is contributed by // Surendra_Gangwar |
Java
// Java implementation of the approach public class GFG { // Function to return the maximum length // of the sub-array such that the // absolute difference between every two // consecutive elements is either 1 or 0 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++; } // Length of the valid sub-array currently // under cosideration int currLen = i - j + 1; // Update the maximum length if (maxlen < currLen) maxlen = currLen; if (j == i) i++; } // Any valid sub-array cannot be of length 1 maxlen = (maxlen == 1) ? 0 : maxlen; // Return the maximum possible length return maxlen; } // Driver code public static void main(String[] args) { int arr[] = { 2, 4 }; System.out.print(getMaxLength(arr)); } } |
Python3
# Python3 implementation of the approach # Function to return the maximum length # of the sub-array such that the # absolute difference between every two # consecutive elements is either 1 or 0 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; # Length of the valid sub-array # currently under cosideration currLen = i - j + 1; # Update the maximum length if (maxlen < currLen) : maxlen = currLen; if (j == i) : i += 1; # Any valid sub-array cannot be of length 1 # maxlen = (maxlen == 1) ? 0 : maxlen; # Return the maximum possible length return maxlen; # Driver code if __name__ == "__main__" : arr = [ 2, 4 ]; n = len(arr) print(getMaxLength(arr, n)); # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum length // of the sub-array such that the // Absolute difference between every two // consecutive elements is either 1 or 0 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++; } // Length of the valid sub-array currently // under cosideration int currLen = i - j + 1; // Update the maximum length if (maxlen < currLen) maxlen = currLen; if (j == i) i++; } // Any valid sub-array cannot be of length 1 maxlen = (maxlen == 1) ? 0 : maxlen; // Return the maximum possible length return maxlen; } // Driver code public static void Main(String []args) { int []arr = { 2, 4 }; Console.Write(getMaxLength(arr)); } } // This code is contributed by Arnab Kundu |
PHP
<?php // PHP implementation of the approach // Function to return the maximum length // of the sub-array such that the // absolute difference between every two // consecutive elements is either 1 or 0 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++; } // Length of the valid sub-array // currently under cosideration $currLen = $i - $j + 1; // Update the maximum length if ($maxlen < $currLen) $maxlen = $currLen; if ($j == $i) $i++; } // Any valid sub-array cannot be of length 1 //maxlen = (maxlen == 1) ? 0 : maxlen; // Return the maximum possible length return $maxlen; } // Driver code $arr = array(2, 4 ); $n = sizeof($arr); echo getMaxLength($arr, $n) // This code is contributed by ita_c ?> |
1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Min and max length subarray having adjacent element difference atmost K
- Maximum length of subarray such that all elements are equal in the subarray
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Minimize the maximum difference between adjacent elements in an array
- Minimize the maximum difference of adjacent elements after at most K insertions
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum length of subarray such that sum of the subarray is even
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Maximum sum subarray of even length
- Length of maximum product subarray
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Find subarray of Length K with Maximum Peak
- Find maximum average subarray of k length
- Length of the longest Subarray with only Even Elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

