in Education by
This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. I have an array of numbers from 1 to 100 (both inclusive). The size of the array is 100. The numbers are randomly added to the array, but there is one random empty slot in the array. What is the quickest way to find that slot as well as the number that should be put in the slot? A Java solution is preferable. 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
You can do this in O(n). Iterate through the array and compute the sum of all numbers. Now, sum of natural numbers from 1 to N, can be expressed as Nx(N+1)/2. In your case N=100. Subtract the sum of the array from Nx(N+1)/2, where N=100. That is the missing number. The empty slot can be detected during the iteration in which the sum is computed. // will be the sum of the numbers in the array. int sum = 0; int idx = -1; for (int i = 0; i < arr.length; i++) { if (arr[i] == 0) { idx = i; } else { sum += arr[i]; } } // the total sum of numbers between 1 and arr.length. int total = (arr.length + 1) * arr.length / 2; System.out.println("missing number is: " + (total - sum) + " at index " + idx);

Related questions

0 votes
    This question exist only because of pure curiosity. Not a homework. Find the fastest way to find two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I've to solve a problem regarding generics which gives me quite the stomache ache. Basically it's supposed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 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
    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
    Find the missing Number?...
asked Feb 13, 2021 in Education by JackTerrance
0 votes
    For instance, given ( 1, 2, 5, 6, 7), i'd like to determine that 3 and 4 are missing? ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    For instance, given ( 1, 2, 5, 6, 7), i'd like to determine that 3 and 4 are missing? ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    How can I check for missing packages and call for installed.packages() to install them smartly? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I'm currently working on a project converting some training materials from one e-learning system to another, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    write a Java program to input 10 non-zero numbers in an array and display the count of even and odd numbers. URGENT! Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have an Array of Objects that need the duplicates removed/filtered. I was going to just override equals ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Which is the correct way to create an array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python"]; III) ... 1. I, III 2. II, III 3. I, II 4. I, II & III...
asked Feb 24, 2021 in Technology by JackTerrance
+1 vote
    Which is the correct way to create an Empty array in JavaScript? I) var myProg = []; II) var myArray = ["C","Java","C++","Python ... = new Array(); I, III II, III I, II I, II & III...
asked Oct 9, 2020 in Technology by JackTerrance
...