Skip to content
Related Articles

Related Articles

Length of longest subsequence consisting of distinct adjacent elements
  • Difficulty Level : Basic
  • Last Updated : 16 Nov, 2020

Given an array arr[], the task is to find the length of the longest subsequence of the array arr[] such that all adjacent elements in the subsequence are different.

Examples:

Input: arr[] = {4, 2, 3, 4, 3}
Output: 5
Explanation:
The longest subsequence where no two adjacent elements are equal is {4, 2, 3, 4, 3}. Length of the subsequence is 5.

Input: arr[] = {7, 8, 1, 2, 2, 5, 5, 1}
Output: 6
Explanation: Longest subsequence where no two adjacent elements are equal is {7, 8, 1, 2, 5, 1}. Length of the subsequence is 5.

Naive Approach: The simplest approach is to generate all possible subsequence of the given array and print the maximum length of that subsequence having all adjacent elements different. 



Time Complexity: O(2N)
Auxiliary Space: O(1)

Efficient Approach: Follow the steps below to solve the problem:

  • Initialize count to 1 to store the length of the longest subsequence.
  • Traverse the array over the indices [1, N – 1] and for each element, check if the current element is equal to the previous element or not. If found to be not equal, then increment count by 1.
  • After completing the above steps, print the value of count as the maximum possible length of subsequence.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that finds the length of
// longest subsequence having different
// adjacent elements
void longestSubsequence(int arr[], int N)
{
    // Stores the length of the
    // longest subsequence
    int count = 1;
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        // If previous and current
        // element are not same
        if (arr[i] != arr[i - 1]) {
 
            // Increment the count
            count++;
        }
    }
 
    // Print the maximum length
    cout << count << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 7, 8, 1, 2, 2, 5, 5, 1 };
 
    // Size of Array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    longestSubsequence(arr, N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the
// above approach
import java.util.*;
 
class GFG{
    
// Function that finds the length of
// longest subsequence having different
// adjacent elements
static void longestSubsequence(int arr[],
                               int N)
{
  // Stores the length of the
  // longest subsequence
  int count = 1;
 
  // Traverse the array
  for (int i = 1; i < N; i++)
  {
    // If previous and current
    // element are not same
    if (arr[i] != arr[i - 1])
    {
      // Increment the count
      count++;
    }
  }
 
  // Print the maximum length
  System.out.println(count);
}
 
// Driver Code
public static void main(String args[])
{
  int arr[] = {7, 8, 1, 2,
               2, 5, 5, 1};
 
  // Size of Array
  int N = arr.length;
 
  // Function Call
  longestSubsequence(arr, N);
}
}
 
// This code is contributed by bgangwar59

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function that finds the length of
# longest subsequence having different
# adjacent elements
def longestSubsequence(arr, N):
     
    # Stores the length of the
    # longest subsequence
    count = 1
 
    # Traverse the array
    for i in range(1, N, 1):
         
        # If previous and current
        # element are not same
        if (arr[i] != arr[i - 1]):
             
            # Increment the count
            count += 1
 
    # Print the maximum length
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 7, 8, 1, 2, 2, 5, 5, 1 ]
     
    # Size of Array
    N = len(arr)
     
    # Function Call
    longestSubsequence(arr, N)
 
# This code is contributed by ipg2016107

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the
// above approach
using System;
  
class GFG{
     
// Function that finds the length of
// longest subsequence having different
// adjacent elements
static void longestSubsequence(int[] arr,
                               int N)
{
   
  // Stores the length of the
  // longest subsequence
  int count = 1;
  
  // Traverse the array
  for(int i = 1; i < N; i++)
  {
     
    // If previous and current
    // element are not same
    if (arr[i] != arr[i - 1])
    {
       
      // Increment the count
      count++;
    }
  }
  
  // Print the maximum length
  Console.WriteLine(count);
}
  
// Driver Code
public static void Main()
{
  int[] arr = { 7, 8, 1, 2,
                2, 5, 5, 1 };
   
  // Size of Array
  int N = arr.Length;
   
  // Function Call
  longestSubsequence(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga

chevron_right


Output: 

6








 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :