1-N numbers are randomly removed from the two numbers, and the two numbers are retrieved at a traversal.

Source: Internet
Author: User
Tags cmath

LearningAlgorithmWrite down some simple questions and do the same.

 

[Question]: 1-n this N number is randomly reduced by two numbers, placed in a [] (disrupt the order), the time complexity of the O (n) algorithm to find the number of reduced 2

[Idea]:If we reduce the number by 1, we all know that the method -- one loop, calculates the sum of a [] and (1 + 2 +... + n) = sum, and then use sum-sum to get the result. However, if there are two numbers, only the sum is not allowed. What can I do? You can use other operations to record more information! Do the four arithmetic operations have addition, subtraction, multiplication, division? Do not always remember +. Forget all other brothers!

 # include "stdafx. H "# include 
  
    # include 
   
     using namespace STD; // The number of 1 --- N is randomly reduced by two and placed in, // find the algorithms with time complexity O (N). // This has a problem. When n reaches a certain level, sum and Mul must cross-border void findtwolostnum (int A [], int N, Int & num1, Int & num2); int _ tmain (INT argc, _ tchar * argv []) {int N; int num1 = 0; int num2 = 0; int A [100]; cout <"input the value of N (<100)" 
    
      N; cout <"input a []" 
     
       A [k];} findtwolostnum (A, N, num1, num2); cout <"the two lost numbers are: \ n "
      
     
    
   
  

 

[Note]: A better method is to replace the second key value sum2 with the annotation part, and then, according to the equations: x + y = sum, x * x + y * Y = sum2, you can find x * y accordingly. The subsequent steps are similar.

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.