+ 1
Problem: find the missing number among 1 to 100.
Given N consecutive numbers from 1 to 100 and one of them is missing. Ex: [ 5, 9, 8, 7 ] here 6 is missing, N = 5 Ex: [ 3, 5, 2, 4, 6 , 7] here two answers are possible 1 and 8, N = 7
3 odpowiedzi
+ 3
1. Find out the smallest
2. Iterate over the array given and check each and every number from the minimum to minimum+N is present or not.
3. Print the missing number along.
+ 2
One way is to sort the list then assign the first value to a variable. Then iterate through the list and check if the variable+1 equals the next variable, if fails then variable+1 is your answer. If the iteration complete without failed, again variable+1 is your answer.
Let list=5,7,8,9
Let t=5
Now iterate through the list and check if t+1==list[index] (Start the iteration from 2nd index). Here the first condition itself fails as 6==7 is false. Now break the loop and t+1 which is 6 is your answer. If the condition is true then increment the value of t by 1 and continue the loop.
For the second example
list=2,3,4,5,6,7
Here the condition will not fails until t become 7 so again the answer is t+1 which is 8 or if you want 1 then do t+1-N
+ 1
One feasible way is to sort the numbers first, then iterate through it, checking whether N[ i ] > N[ i - 1 ] as you go (start from index 1). If the check fails, you got a missing number ...