CSDN discusses how to identify missing numbers

Source: Internet
Author: User

[Problem]

There is a set of numbers, from 1 to n, which reduces the number of three, the order is also disrupted, placed in an array of n-3


Please find the missing number. It is better to have a program, and the best algorithm is faster.
Suppose n = 10000

[Solution]

Method 1:

/* There is a set of numbers, from 1 to n, which reduces the number of 3, the order is also disrupted, put in an array of n-3 please find out the Lost number, it is best to have a program, the best algorithm is faster. Suppose n = 10000. Suppose there is an array. A [N-3] method: space to time open an array int.
B [N + 1] and initialize to 0 to traverse array A. For example, if A [I] = 250, B [250] = 1 until array A is traversed, then the Lost number is on B [x] = 0, and then traverse array B to find B [x] = 0, B [y] = 0, B [z] = 0. then the efficiency of x, y, and z is n */

 

<Pre name = "code" class = "cpp"> void missnum (int A, int n-3)
{
Int B [n + 1] = {0 };
For (int I = 0; I <n-3; I ++)
{
B [A [I] = 1;
}
For (int I = 1; I <n + 1; I ++)
{
If (B [I]! = 1)
Printf ("% d", I); // number of output loss
}
}


From here I am

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.