There are 1 to 10 W of the 10 W number, remove 2 and disrupt the order, how to find the two numbers

Source: Internet
Author: User

There are 1 to 10 W of the 10 W number, remove 2 and disrupt the order, how to find the two numbers
Description: there are 1 to 10 W numbers, remove 2 and disrupt the order, and how to find the two numbers.
1. Bitmap method:
Assume that the array to be processed is a [10w-2]
Define an array B [10 W]. Here we assume that each element in B occupies 1 bit and is initialized to all 0
For (I = 0; I <10w-2; I ++)
{
B [A [I] = 1
}
Then, the zero element in B is the missing data.
This method is very efficient and is one of the most commonly used algorithms in computers.
2. Calculate the sum of sum and sum of squares between 1 and 10 W.
Then calculate the sum and sum of squares of the given number.
The sum and sum of squares of the two numbers are obtained.
So we have
X + y = N
X ^ 2 + y ^ 2 = m
The values of X and Y can be obtained by solving the equation.
Sum and sum of squares can get the result, but it is possible that the sum of squares has a large amount of computing (64-bit int will not overflow)

Const int size = 1000;

Int main (INT argc, char * argv [])
{
Int A [] = {2, 3, 4, 5, 6, 7 };
Int B [size] = {0 };
For (INT I = 0; I <sizeof (a)/sizeof (A [0]); I ++ ){
B [A [I] = 1;
}
For (Int J = 0; j <size; j ++ ){
If (B [J] = 0 ){
Printf ("% d \ t", J );
}

}

Return 0;
}

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.