Loading content...
You are given an integer array nums arranged in non-decreasing order. Your task is to modify the array in-place so that each unique value appears at most twice, while preserving the original relative ordering of elements.
Since certain programming languages do not allow resizing arrays, the valid result should occupy the first k positions of the original array. The function must return the value k, representing the count of elements in the final processed array.
Important: Only the first k elements of the modified array will be evaluated. Any values beyond position k are considered irrelevant and will not affect the correctness of your solution.
The solution must operate directly on the input array without allocating a separate array for output. Your algorithm should use only O(1) auxiliary space beyond the input array itself.
Evaluation Criteria:
The validation system will verify your solution as follows:
int[] nums = [...]; // Input array
int[] expectedResult = [...]; // Expected array with correct length
int k = limitOccurrences(nums); // Your implementation
assert k == expectedResult.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedResult[i];
}
Your solution passes if all assertions succeed.
nums = [1,1,1,2,2,3]k = 5, nums = [1,1,2,2,3,_]The function returns k = 5. The first five positions contain [1,1,2,2,3]. The element '1' appeared three times originally but now appears only twice. The underscore represents a value that doesn't matter and won't be checked.
nums = [0,0,1,1,1,1,2,3,3]k = 7, nums = [0,0,1,1,2,3,3,_,_]The function returns k = 7. The first seven positions contain [0,0,1,1,2,3,3]. The value '1' originally appeared four times but is now limited to two occurrences. The two underscores represent irrelevant values.
nums = [1,1,2,2,3,3]k = 6, nums = [1,1,2,2,3,3]The function returns k = 6. Since no element appears more than twice, the array remains unchanged. All six elements are valid in the output.
Constraints