LeetCode 287. Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 andn (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.


The most straight forward way is to use hashmap..... However, the note said O(1) space allowed. hmm, after reading the note, there is a clue...

There are a few algorithm complexity less then O(n2). Sort -> nlgn. In this case, we need to sort the inputs first and then use binary search to find the element.


Suppose, we have a sorted array. How to apply binary search is the main trick here.

So, let's make several examples

[1, 2, 2, 2, 3]:

  First Round: left = 0, right = 4, mid = 2, nums[mid] = 2  < mid + 1.... .We thus know that the duplicate number is on the left. Thus, right = mid;

  Second Round: left = 0, right = 2, mid = 1, nums[mid] = 2 == mid + 1, we thus know that the duplicate number is on the right. Thus, left = mid;

  Third Round: left = 1, right 2,  ---> in this case, only two elements left, the duplicate is in them two.We dont need to apply binary search anymore. Since, left(1) have been verified in Secound Round. Thus, Duplicate = nums[right].


[1, 2, 3, 4, 4]

  First Round: left = 0, right = 4, mid = 2, nums[mid]  = 3 == mid + 1, we thus know that the duplicate number is on the right. Thus, left = mid;

  Second Round: left = 2, right = 4, mid = 3, nums[mid] = 4 == mid + 1, we thus know that the duplicate number is on the right. Thus, left = mid;

 Third Round: left = 3, right = 4 --> in this case, there are only two elements left, the duplicate is in them two.Since we know that 3 is not the duplicate from the second round. Then, nums[right] must be the duplicate.


[1, 2, 4, 5, 7, 7, 7, 7, 8]

  First Round: left = 0, right = 8, mid = 4, nums[mid] = 7 > mid + 1, we thus know that the duplicate number must be on the right. thus, left = mid;

  Second Round: left = 4, right = 8, mid = 6, nums[mid] = 7 == mid + 1, we thus know that the duplicate must be on the right. Thus, left = mid;

  Third Round: left = 6, right = 8, mid = 7, nums[mid] = 7 < mid + 1, we thus know that the duplicate must be on the left. Thus, right = mid;

  Fourth Round: left = 6, right = 7. ---> in this case, there are only two elements left.The duplicate must be in them two. Since we know that nums[6] is not the duplicate from the second round,then, nums[right] is the duplicate.


#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int findDuplicate(vector<int>& nums) {
    if(nums.size() <= 1) return -1;
    sort(nums.begin(), nums.end());   // nlgn time complexity.
 
    // binary search then.
    int left = 0, right = nums.size() - 1;
    while(left < right - 1) {
        int mid = left + (right - left) / 2;
        if(nums[mid] >= mid + 1) left = mid;
        else right = mid;
    }
    return nums[right];
}

int main(void) {
    vector<int> nums{7, 9, 7, 4, 2, 8, 7, 7, 1, 5};
    int target = findDuplicate(nums);
    cout << target << endl;
}

Second Round:

Read the Question again, Found that I was wrong on the first try. "There is a requirement that the array is read-only"

Solving it using Bit manipulation  instead. Time Complexity O(N),  Space O(1)

#include <iostream>
#include <vector>
using namespace std;

/*
  Array nums containing n+1 integers, where each integer is between 1 and n.
  Find the only duplicate.
  For example [1, 2, 3, 1], n = 3.
*/
int findDuplicate(vector<int>& nums) {
  int duplicate = 0;
  int res = 0;
  for(int i = 0; i < 32; ++i) {
    int a = 0, b = 0;
    int bitMask = (1 << i);
    for(int k = 0; k < nums.size(); ++k) {
      a += (bitMask & nums[k]);
    }
    for(int k = 1; k <= nums.size() - 1; ++k) {
      b += (bitMask & k);
    }
    if(a > b) {
      res = res | bitMask;
    }
  }
  return res;
}

int main(void) {
  vector<int> nums{2, 2, 3, 1};
  cout << findDuplicate(nums) << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值