FastPrepMissing and Repeated Number

Missing and Repeated Number

Commvault logoCommvault● EasyFULLTIMEONSITE INTERVIEW

Problem statement

An unsorted integer array nums has length n and contains values from 1 through n. Exactly one value appears twice, exactly one value is missing, and every other value appears once.

Return an array containing the repeated value followed by the missing value. Solve the task in O(n) time and O(1) extra space without changing nums.

Function

findRepeatedAndMissing(nums: int[]) → int[]

Examples

Example 1

nums = [3,1,2,5,3]return = [3,4]

The value 3 occurs twice and 4 is absent.

Example 2

nums = [1,1]return = [1,2]

The repeated value is 1 and the missing value is 2.

Example 3

nums = [2,3,2,4]return = [2,1]

The missing value can be smaller than the repeated value.

Constraints

  • 2 <= n <= 200000.
  • 1 <= nums[i] <= n.
  • Exactly one value occurs twice and exactly one value is absent.

More Commvault problems

See Commvault hiring insights
Saved
public int[] findRepeatedAndMissing(int[] nums) {
  // Write your code here.
}
nums[3,1,2,5,3]
expected[3,4]
Checking account…