给定数组arr [] ,任务是找到数组arr []的最长子序列的长度,以使子序列中的所有相邻元素都不同。
例子:
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.
天真的方法:最简单的方法是生成给定数组的所有可能的子序列,并打印所有相邻元素都不同的子序列的最大长度。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:请按照以下步骤解决问题:
- 将count初始化为1以存储最长子序列的长度。
- 在索引[1,N – 1]上遍历数组,并为每个元素检查当前元素是否等于前一个元素。如果发现不相等,则将count递增1 。
- 完成上述步骤后,将count的值打印为子序列的最大可能长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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;
}
Java
// 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
Python3
# 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
C#
// 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
输出:
6
时间复杂度: O(N)
辅助空间: O(1)