AdSense

Wednesday, July 6, 2016

Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.


At first glance, we will think of using a HashSet, which then makes the problem really easy. However, the problem requires us NOT to use extra space. However, it mentions that there are n elements in an array with n + 1 length, which means there must be one that is duplicated. To find the duplicated one, we can utilize pigeonhole principle.

For a sorted array, each element should be in the position of itself, i.e., nums[i] = i, if no element is duplicated. What we can do is to "sort" the array by swapping elements that do not belong to the position. If there is a correct element in the position that the current one is supposed to be in, then we know this is a duplicated one.

If every one is in the correct space and we know there is a duplicated one, then we know it is in pos = 0.

public int findDuplicate(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != i) {
                while (nums[i] != i) {
                    if (nums[i] == nums[nums[i]])
                        return nums[i];
                    swap(nums, i, nums[i]);
                }
            }
        }
        return nums[0];
    }
    
    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

No comments:

Post a Comment