- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 subsequence with difference between adjacent elements as either 0 or 1 | Set 2 in C++

We are given an array of any size and the task is to find the subsequence in 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 subsequence with difference between adjacent elements as either 0 or 1 is: 3

**Explanation** − The subsequence of adjacent elements in an array with difference as 0 or 1 are {2, 1}. Therefore, the maximum length of subsequence is 2.

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

**Output** − Maximum length subsequence with difference between adjacent elements as either 0 or 1 is: 3

**Explanation** − The adjacent elements in an array with difference as 0 or 1 is {7, 6, 5}.. Therefore, the maximum length of subsequence 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 a temporary variable maximum and set it to 0 and take another temporary variable i and set it to 0 as well

Create a variable un_map of type unordered_map

Start loop while till i less than size

Inside the loop, set len to 0 and check if un_map.find(arr[i]-1) != un_map.end() && len < un_map[arr[i]-1] then set len to len = un_map[arr[i]-1]

check if un_map.find(arr[i]) != un_map.end() && len < un_map[arr[i]] then set len to len = un_map[arr[i]]

check if un_map.find(arr[i]+1) != un_map.end() && len < un_map[arr[i]+1] then set len to len = un_map[arr[i]+1]

Now set un_map[arr[i]] = len + 1

Check if maximum less than un_map[arr[i]] then set maximum with un_map[arr[i]]

Increment the value of i

Return maximum

Print the result

## Example

#include <bits/stdc++.h> using namespace std; //calculate the maximum subsequence int maximum_adj(int arr[], int size){ int maximum = 0, i = 0; unordered_map<int, int> un_map; while(i < size){ int len = 0; if (un_map.find(arr[i]-1) != un_map.end() && len < un_map[arr[i]-1]){ len = un_map[arr[i]-1]; } if (un_map.find(arr[i]) != un_map.end() && len < un_map[arr[i]]){ len = un_map[arr[i]]; } if (un_map.find(arr[i]+1) != un_map.end() && len < un_map[arr[i]+1]){ len = un_map[arr[i]+1]; } un_map[arr[i]] = len + 1; if (maximum < un_map[arr[i]]){ maximum = un_map[arr[i]]; } i++; } return maximum; } int main(){ int arr[] = {2, 3, 1, 7, 5, 6, 7, 8}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Maximum length subsequence with difference between adjacent elements as either 0 or 1 are: "<< maximum_adj(arr, size); return 0; }

## Output

Maximum length subsequence with difference between adjacent elements as either 0 or 1 are: 4

- Related Questions & Answers
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 in C++
- Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++
- Problem with division as output is either 0 or 1 when using ifthenelse condition in ABAP program
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum determinant of a matrix with every values either 0 or n in C++
- Maximum sum of difference of adjacent elements in C++
- Set conditions for columns with values 0 or 1 in MySQL
- Difference Between RAID 0 and RAID 1
- Difference Between exit(0) and exit(1)
- Maximum value with the choice of either dividing or considering as it is in C++
- Difference between Hadoop 1 and Hadoop 2
- Difference Between COCOMO 1 and COCOMO 2
- Design NFA with Σ = {0, 1} and accept all string of length at least 2.
- Maximum decreasing adjacent elements in JavaScript
- Maximum value with the choice of either dividing or considering as it is in C++ program