
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 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 sum of difference of adjacent elements in C++
- Maximum determinant of a matrix with every values either 0 or n in C++
- Maximum set bit sum in array without considering adjacent elements in C++
- Maximum value with the choice of either dividing or considering as it is in C++
- Maximum decreasing adjacent elements in JavaScript
- Maximum value with the choice of either dividing or considering as it is in C++ program
- Maximum sum subsequence with at-least k distant elements in C++
- Set conditions for columns with values 0 or 1 in MySQL
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- Calculate difference between adjacent elements in given list using Python
- Maximum sum subsequence with at-least k distant elements in C++ program
