- 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++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Problem with division as output is either 0 or 1 when using ifthenelse condition in ABAP program
- Maximum determinant of a matrix with every values either 0 or n in C++
- Maximum sum of difference of adjacent elements in C++
- Maximum value with the choice of either dividing or considering as it is in C++
- Difference between while(1) and while(0) in C/C++
- Difference Between exit(0) and exit(1)
- Difference Between RAID 0 and RAID 1
- Maximum value with the choice of either dividing or considering as it is in C++ program
- Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n in C++
- Set conditions for columns with values 0 or 1 in MySQL
- Difference between while(1) and while(0) in C language
- Difference between Hadoop 1 and Hadoop 2

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

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.

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

#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; }

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

Advertisements