"Algorithm"--1 to N to reduce a number, order is scrambled to find the missing number

Source: Internet
Author: User
Problem

There are 0-n this n+1 number, but one lost a number, how can I find out which number lost?

Five different methods

1) Subtract the sum of the current input data with 1+2+...+n. Time complexity: O (N) spatial complexity: O (1) "Easy Overflow"

2) Divide the total product of the current input data by 12... *n. Time complexity: O (N) spatial complexity: O (1) "Easy Overflow"

3) Use 1^2^...^n results to input data individually or at the current date. Complexity of Time: O (n) spatial complexity: O (1)

4) sort the input data and iterate through it from beginning to end. Time complexity O (nlogn) space complexity O (1)

5) hash The input data and iterate through it from beginning to end. Time complexity O (n) spatial complexity O (n)

"Algorithm"--1 to N to reduce a number, order is scrambled to find the missing number

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.