Longest Uniform Subarray — Problem Statement & Solution Guide
Problem Description
Given a sequence of integers nums, determine the length of the longest contiguous segment where all elements are identical. If the sequence is empty, return 0.
Examples
Input
[1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3]
Output
3
Explanation: Step-by-step: with input [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3], we initialize a variable `maxLen` to 1 and a variable `currLen` to 1. We then iterate through the sequence, incrementing `currLen` for each identical element and updating `maxLen` if `currLen` is greater than `maxLen`. Finally, we return `maxLen`, which is 3.
Input
[1, 2, 3, 4, 5]
Output
1
Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we initialize a variable `maxLen` to 1 and a variable `currLen` to 1. We then iterate through the sequence, incrementing `currLen` for each identical element and updating `maxLen` if `currLen` is greater than `maxLen`. Since all elements are distinct, `currLen` remains 1, and we return `maxLen`, which is 1.
Constraints
- 1 <= seq length <= 10^5
- -10^9 <= seq elements <= 10^9
- seq is a list of integers
- seq can be empty
- seq elements are not guaranteed to be distinct
Optimal Approach & Strategy
The optimal approach involves scanning the sequence once and keeping track of the current uniform subarray length. When a different element is encountered, the length of the current uniform subarray is compared with the maximum length found so far and updated if necessary. This approach has a time complexity of O(n) and a space complexity of O(1), where n is the length of the sequence.
Brute Force Approach
The brute force approach involves iterating over the sequence and comparing each element with its adjacent elements to identify uniform subarrays. This can be achieved by using nested loops to generate all possible subarrays and checking each one for uniformity. However, this approach is inefficient for large sequences due to its high time complexity.
Verified Code Solutions
function solution(nums) {
if (nums.length === 0) return 0;
let maxLen = 1;
let currLen = 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] === nums[i - 1]) {
currLen++;
maxLen = Math.max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
}class Solution {
public:
int solution(vector<int>& nums) {
if (nums.size() == 0) return 0;
int maxLen = 1;
int currLen = 1;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] == nums[i - 1]) {
currLen++;
maxLen = max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
}
};class Solution {
public int solution(int[] nums) {
if (nums.length == 0) return 0;
int maxLen = 1;
int currLen = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i - 1]) {
currLen++;
maxLen = Math.max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
}
}def solution(nums):
if not nums:
return 0
max_len = 1
curr_len = 1
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
curr_len += 1
max_len = max(max_len, curr_len)
else:
curr_len = 1
return max_lenfunction solution(nums) {
if (nums.length === 0) return 0;
let maxLen = 1;
let currLen = 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] === nums[i - 1]) {
currLen++;
maxLen = Math.max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
}Asked in Top Tech Interviews
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.