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

 Live Demo

#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

Updated on: 03-Aug-2020

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements