AdSense

Sunday, October 9, 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 element 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 extra space.
  3. Your runtime complexity should be less than O(n2).


The most straightforward way is to utilize pigeon hole theorem: keep swapping elements so that nums[i] = [i] until we find nums[nums[i]] = nums[i], then nums[i] is the duplicate one. If everything is in place, return nums[0].

BUT, the question requires no modification to the array, and no extra space. So we cannot use swap or construct an external array. There is a very interesting solution online that utilizes the solution in find cycle in linkedlist problem. So we consider each index as a list node, and the next node is its element, i.e., i -> nums[i]. Now we can construct a linked list in our mind. Since there is a duplicate node, we know there must exist a cycle. Now we can use the solution of Linked list Cycle II to solve the problem.

public int findDuplicate(int[] nums) {
        int slow = nums[0], fast = nums[nums[0]], t = 0;
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        
        fast = 0;
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }



1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete