The Lucky Streak
Anjali, a student at UptoSkills Arena, loves finding "lucky streaks" in sequences of numbers. A lucky streak is defined as a contiguous subarray where all elements are distinct. She wants to find the longest possible lucky streak in a given sequence of integers. Help Anjali find the length of the longest lucky streak. If there are multiple streaks of the same maximum length, choose the one that starts at the earliest index.
Input
The first line contains an integer N, the size of the sequence. The second line contains N space-separated integers representing the sequence.
Output
A single integer representing the length of the longest lucky streak. If multiple streaks have the same maximum length, output the length of the one starting at the earliest index.
Constraints
1 <= N <= 200000 -10^9 <= a_i <= 10^9
Example 1
5 1 2 3 2 1
3
The longest streak with distinct elements is [1, 2, 3] of length 3.
Example 2
7 1 1 1 1 1 1 1
1
The longest streak with distinct elements is [1] of length 1.
Hints
Similar problems
- Longest Subarray with At Most K Duplicate PairsMedium
- Distinct Elements in a Sliding WindowMedium
- Count Matching PairsMedium
- Count Subarrays with Distinct ElementsMedium
- Count Elements with Equal NeighborsMedium
- Count Pairs with Equal ProductMedium
More topics: Arrays problems, Hash Map and Hashing problems, Sliding Window problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 1 2 3 2 1
7 1 1 1 1 1 1 1