Algorithm (2) Find all Numbers disappeared in an Array

Source: Internet
Author: User

title : An integer array satisfies 1<=a[i]<=n (n is the length of the array), some elements appear once, some elements appear two times, and numbers that do not appear in the "1,n" interval are found in the array a[i]. For example, enter "4,3,2,7,8,2,3,1" and Output "5,6". Time complexity requirement is O (n), Space complexity requirement O (1)

Ideas : see many netizens say with set data structure to do, this violates the space complexity requirements, so sort or to do local. Iterate the array, put this number where it should be: for example, a[0]=4, then we put 4 to a[3], and then put the a[3] place 7 to a[6] place, place a[6] Place 3 a[2] place, put a[2] place 2 at a[1] Place, Dizzy Bar

Do this:

Step1: 7, 3,2,4, 8,2,3,1

Step2:3, 3,2,4,8,2,7, 1

Step3:2, 3,3, 4,8,2,7,1

Step4:3,2, 3,4,8,2,7,1

Step4. 1: Now think of exchange 3 and 3, but found heavy, then 3 or stay in the position,

STEP5:

Answer :

Https://github.com/honpey/codebox/blob/master/leetcode/array/p448.cpp

related :

For array 1<=a[i]<=n,

Algorithm (2) Find all Numbers disappeared in an Array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.