BackmediumArraysUber

Longest Distinct Circular Subsequence Solution

Problem Statement

Given a circular array of integers elements, determine the length of the longest contiguous subsequence that contains no duplicate elements.

Example 1
Input
[1, 2, 3, 1, 2, 3]
Output
3

Explanation: Step-by-step: Given the input [1, 2, 3, 1, 2, 3], we first find the longest contiguous subsequence with no duplicates. The longest contiguous subsequence with no duplicates is [1, 2, 3]. The length of this subsequence is 3.

Example 2
Input
[1]
Output
1

Explanation: Step-by-step: Given the input [1], we first find the longest contiguous subsequence with no duplicates. The longest contiguous subsequence with no duplicates is [1]. The length of this subsequence is 1.

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 Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Distinct Circular Subsequence — Problem Statement & Solution Guide

ArraysMediumlongest-substring-without-repeating-characters
TimeO(n)
|
SpaceO(n)

Problem Description

Given a circular array of integers elements, determine the length of the longest contiguous subsequence that contains no duplicate elements.

Examples

Example 1

Input

[1, 2, 3, 1, 2, 3]

Output

3

Explanation: Step-by-step: Given the input [1, 2, 3, 1, 2, 3], we first find the longest contiguous subsequence with no duplicates. The longest contiguous subsequence with no duplicates is [1, 2, 3]. The length of this subsequence is 3.

Example 2

Input

[1]

Output

1

Explanation: Step-by-step: Given the input [1], we first find the longest contiguous subsequence with no duplicates. The longest contiguous subsequence with no duplicates is [1]. The length of this subsequence is 1.

Constraints

  • The length of the input array is between 1 and 1000.
  • Each element in the array is an integer between 1 and 10000.

Optimal Approach & Strategy

The optimized approach utilizes a sliding window technique in conjunction with a hash set to efficiently track unique elements within the current window, allowing for a significant reduction in time complexity.

Brute Force Approach

The brute-force approach involves checking every possible subsequence of the given array, which results in a time complexity of O(n²), making it inefficient for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestDistinctCircularSubsequence(elements) {
  if (elements.length === 0) return 0;
  let maxLen = 0;
  let set = new Set();
  let left = 0;
  for (let right = 0; right < elements.length; right++) {
    while (set.has(elements[right])) {
      set.delete(elements[left]);
      left++;
    }
    set.add(elements[right]);
    maxLen = Math.max(maxLen, right - left + 1);
  }
  return Math.max(maxLen, 1);
}

Asked in Top Tech Interviews

Uber

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.