in Education by
This question exist only because of pure curiosity. Not a homework. Find the fastest way to find two missing number in an array 1..n So, In a related post: Quickest way to find missing number in an array of numbers I found out that you can do this pretty quickly by summing up and substracting total. but what about 2 numbers? So, our options are: Sequential search Summing up items, substracting from total for all items from 1..n, then searching all possible cases. Anything else? Possible to have O(n) solution? I found this in ruby section of one of the websites, but any language is considered (unless there are some specific things for a language) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Find the sum of the numbers S=a1+...+an. Also find the sum of squares T=a1*a1+...+an*an. You know that the sum should be S'=1+...+n=n(n+1)/2 You know that the sum of squares should be T'=1^2+...+n^2=n(n+1)(2n+1)/6. Now set up the following system of equations x+y=S'-S, x^2+y^2=T'-T. Solve by writing x^2+y^2=(x+y)^2-2xy => xy=((S'-S)^2-(T'-T))/2. And now the numbers are merely the roots of the quadratic in z: z^2-(S'-S)z+((S'-S)^2-(T'-T))/2=0.

Related questions

0 votes
    This question's answers are a community effort. Edit existing answers to improve this post. It is not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    This is what I have so far: myArray.map!{ rand(max) } Obviously, however, sometimes the numbers ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I declare steps that need to be done in order to install wordpress in an array in the beginning of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    In Ruby, Dir.glob("**/*.rb") (for instance) doesn't traverse symlinked directories. Is it ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    first..sorry for my poor english and my big noob question..the problem is... I need create a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    As the title suggests, I'm just looking for a way of pressing a button in Shoes without clicking ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Let's say I have one variable, directory_list, which I define and set in a ruby_block named get_directory_list. Can ... woohoo" end end Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I had to delete all the rows from a log table that contained about 5 million rows. My initial try ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I had to delete all the rows from a log table that contained about 5 million rows. My initial try ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    What is the fastest way to improve a poor credit score?...
asked Oct 10, 2020 in Credit by Editorial Staff
0 votes
    Given an array of integers, which can contain both +ve and -ve numbers. I've to maximize the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have an array that has multiple numbers in it. I want the function to return another array of maximum 3 ... { dartsToFinishArrTotal += dartsToFinishArr[n]; } } if (output...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have an array that has multiple numbers in it. I want the function to return another array of maximum 3 ... { dartsToFinishArrTotal += dartsToFinishArr[n]; } } if (output...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
0 votes
    While shuffling a pack of 52 playing cards, 2 are accidentally dropped. Find the probability that the missing cards to ... . 27 51 27 Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
...