There is a set of numbers, which reduce the number from 1 to n, and the order is also disordered. Put it in a n-1 array. Please find the missing number.
- Use 1 + 2 +... + N (that is, n (n + 1)/2) to subtract the sum of the current input data. The time complexity is O (n), and the space complexity is O (1). The disadvantage is that it is easy to overflow. To mitigate the overflow, add and subtract the edge when the value is 1 + 2 +... + N. If the array is A, then this can calculate 1-A [0] + 2-A [1] +... + (n-1)-A [N-2] + N.
- Use 1*2 *... * n to divide the product of the current input data. Time complexity O (N) and space complexity O (1 ). The disadvantage is also easy to overflow. The solution is as follows: 1/A [0] * 2/A [1]... (n-1)/A [N-2] * n (use floating point Division ).
- Use 1 ^ 2 ^... ^ n to return an exception one by one or the current input data. The time complexity is O (n), and the space complexity is O (1 ). This is a perfect solution, and there is no overflow problem.
- Sort the input data and traverse it from start to end. Time complexity O (nlgn), space complexity O (1) (also depends on SortingAlgorithm). The disadvantage is obvious, and the time complexity is too high.
- Hash the input data and traverse it from scratch. Time complexity O (N) and space complexity O (n ).
Question extension 1: What if two numbers are missing?
Assume that the two numbers are X and Y.
- Using 1 + 2 +... + N minus the sum of the current input data, you can get x + y, and subtract the sum of squares of the current input data. X and Y can be obtained by combining two equations. Time complexity O (N) and space complexity O (1 ). The problem is that it is easy to overflow.
- Use 1 + 2 +... + N minus the sum of the current input data, you can get X + Y. Use 1*2 *... * dividing N by the product of input data produces XY. Both x and y can be obtained. Time complexity O (N) and space complexity O (1 ). The disadvantage is that it is easy to overflow.
- Sort the input data and traverse it from start to end. Time complexity O (nlgn) and space complexity O (1 ).
- Hash the input data and traverse it from start to end. Time complexity O (N) and space complexity O (n ).
- Use the result of 1 ^ 2 ^... ^ n one by one or the current input data to obtain x ^ y. Because X and Y are not equal, at least one of the results of x ^ y is 1. Based on the difference of this bit (0 or 1), we divide the input data and 1, 2,..., n into two heaps. Then, the two stacks are exclusive or respectively to get X and Y.
Extended question:-1000 put in an array containing 1001 elements. Only one unique element is repeated, and only one other element appears. Each array element can only be accessed once. design an algorithm and find it. No auxiliary storage space is required.
The solution to this problem is similar. the result obtained by using 1 ^ 2 ^... ^ 1000 is different one by one or the 1001 elements entered.
Similar issues:
- Given n numbers, only one of them has an odd number and the other has an even number. Use the linear time constant space to find the number that appears an odd number.
- Given n numbers, only two of them appear odd times, And the rest appear even times. Use the linear time constant space to find the two numbers with an odd number.