Interview Questions: 1 ~ N-1 has n numbers, which are ordered to find the number of duplicates.

Source: Internet
Author: User

There are multiple solutions to this question:
First: Mathematical Solution
Example
1 2 3 4 = 10
1 2 3 3 = 9
Then we can calculate 4-(10-9) = 3, and 3 is repeated.
So we only need to get 1 ~ The sum of N-1 can be calculated using the Gaussian formula, but the premise is to know the sum, then the time complexity of traversing the array is O (n ).
If this sequence is not ordered, this method is still common.

Second: half-Lookup
Because this sequence is ordered, it is more appropriate to use the semi-query method.
Array [n/2-1] = n/2? If this parameter is not met, you can search for it later. The time complexity is O (logn)

Category 3: X-copy search 
How many folds? This is very interesting. One of the interview questions is similar to this one.

[Question] There are two cups, 100 floors. The Cup can be broken on a certain layer. The minimum test shows which layer (bottom layer) can be broken?

The Cup can be dropped from the first layer to a layer, up to 100 times, or two layers and two layers, up to 50 times, or three layers and three layers, up to 33 times. Can it be faster?
At lunch, a colleague said that he could use the Inverse thinking of the binary method to do this, for example, 2-> 4-> 8-> 16-> 32-> 64-> 100. The span is getting bigger and bigger, it may take up to 30 times. Although this method is not the desired result, it provides us with an inspiration that we can find a suitable degree. For example, if you can directly find a building and fail to return, you can save a lot of floors. This idea is very important.

What if we accumulate? 1 3 6 10 15 21 28 36 45 55 66 78 91 100 at most 12 + 12 = 24 times can it be faster?
Decrease in reverse order? The most frequently used layer is the first layer! 14 27 39 50 60 69 77 84 90 95 99... omg this... Can it be faster?
If X + x * x> = 2n, the optimal number of times is X.
The number of duplicates is not exactly the same as the number of broken cups, because we are not the only two chances to break the two cups. We have n chances. Therefore, if 14 is broken, it can be 5 9 13-> 3-> 1.

Code
Static void getrepeater (int s, int X, int N, int [] array)
{
If (x = 1)
{
Console. Write (array [s-1]);
Console. Read ();
Return;
}
If (array [s-1]! = S)
{
N = X;
X = getx (X );
S-= X;
Getrepeater (S, X, N, array );
}
Else
{
X --;
Getrepeater (S + X, X, N-X, array );
}
}
Static int getx (int n)
{
VaR x = 1;
While (x + x * x <2 * n)
{
X ++;
}
Return X;
}
Static void main (string [] ARGs)
{
Int [] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
Int n = array. length;
Int x = getx (N );
Int S = X;
Getrepeater (S, X, N, array );
}

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.