BackmediumArrays Swiggy

Galactic Transmission Sequence Solution

Problem Statement

Given an array of integers, find the length of the longest subsequence without duplicates in a circular buffer.

Example 1
Input
[3, 7, 2, 9, 4]
Output
4

Explanation: Step 1: Initialize an empty set to store unique elements and a variable to store the maximum length of the subsequence without duplicates. We start with the first element of the array, which is 3. The set contains [3] and the maximum length is 1. Step 2: Iterate over the rest of the array. When we encounter the element 7, we add it to the set and the maximum length becomes 2. Step 3: When we encounter the element 2, we add it to the set and the maximum length becomes 3. Step 4: When we encounter the element 9, we add it to the set and the maximum length becomes 4. Step 5: When we encounter the element 4, we add it to the set and the maximum length becomes 5. However, we should not consider the last element 4 because it is a duplicate. Therefore, the maximum length is 4.

Example 2
Input
[8, 6, 8, 5, 2]
Output
4

Explanation: Step 1: Initialize an empty set to store unique elements and a variable to store the maximum length of the subsequence without duplicates. We start with the first element of the array, which is 8. The set contains [8] and the maximum length is 1. Step 2: Iterate over the rest of the array. When we encounter the element 6, we add it to the set and the maximum length becomes 2. Step 3: When we encounter the element 8, we add it to the set but do not increment the maximum length because it is a duplicate. Step 4: When we encounter the element 5, we add it to the set and the maximum length becomes 3. Step 5: When we encounter the element 2, we add it to the set and the maximum length becomes 4.

Constraints

  • The length of the input array is between 1 and 1000.
  • Each element in the array is an integer between 1 and 10000.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free