- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++

We are given an array of any size and the task is to find the subarray of the given array with the elements having difference between adjacent elements as 0 or 1.

**Input** − int arr[] = { 2, 1, 5, 6, 3, 4, 7, 6}

**Output** − Maximum length subarray with difference between adjacent elements as either 0 or 1 are − 2

**Explanation** − The adjacent elements in an array with difference as 0 or 1 are {2, 1}, {5, 6}, { 3, 4} and {7.6}. Therefore, the maximum length of subarray is 2.

**Input** − int arr[] = { 2, 1, 7, 6, 5}

**Output** − Maximum length subarray with difference between adjacent elements as either 0 or 1 are − 3

**Explanation** − The adjacent elements in an array with difference as 0 or 1 are {2, 1} and {7, 6, 5}.. Therefore, the maximum length of subarray is 3.

## Approach used in the below program is as follows

- Input an array of type integers which can contain positive as well as negative elements.
- Calculate the size of an array and pass an array and size to the function for further functionality.
- Take temporary variable i and set it to 0 and maximum variable and set it to 0.
- Start loop while from i till size of an array.
- Inside the loop, set j to i
- Start another loop which will calculate the subarray with the adjacent elements whether it is 0 or not.
- Inside the loop, increment the value of i.
- Set temp to i-j+1
- Check if maximum is less than temp then set maximum to temp.
- Check whether if j is equals to i then increment the value of i
- Return the maximum
- Print the result.

## Example

#include<bits/stdc++.h> using namespace std; //function to calculate maximum Length subarray with // 0 or 1 difference between adjacent elements int maximum_diff(int arr[], int size){ int i = 0; int maximum = 0; while (i < size){ int j = i; while (i+1 < size && (abs(arr[i] - arr[i + 1]) == 1 || abs(arr[i] - arr[i + 1]) == 0)){ i++; } int temp = i - j + 1; if (maximum < temp){ maximum = temp; } if(j == i){ i++; } } return maximum; } int main(){ int arr[] = { 2, 1, 5, 6, 3, 4, 7, 6}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Maximum length subarray with difference between adjacent elements as either 0 or 1 are: "<< maximum_diff(arr, size); }

## Input

Maximum length subarray with difference between adjacent elements as either 0 or 1 are: 2

- Related Articles
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 in C++
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 | Set 2 in C++
- Maximum sum of difference of adjacent elements in C++
- Maximum determinant of a matrix with every values either 0 or n in C++
- Maximum length subarray with LCM equal to product in C++
- Maximum value with the choice of either dividing or considering as it is in C++
- Problem with division as output is either 0 or 1 when using ifthenelse condition in ABAP program
- Maximum value with the choice of either dividing or considering as it is in C++ program
- Maximum Subarray Sum Excluding Certain Elements in C++
- Contiguous subarray with 0 and 1 in JavaScript
- Find maximum average subarray of k length in C++
- Maximum Subarray Sum Excluding Certain Elements in C++ program
- Maximum decreasing adjacent elements in JavaScript
- Maximum length of subarray such that sum of the subarray is even in C++
- Maximum length of segments of 0’s and 1’s in C++

Advertisements