Instead of running a loop for expectedSum, just use the formula n*(n+1)/2 where n is size of array. Significantly reduces the running time for larger arrays.
@CodeWithZiАй бұрын
You are correct that also works! Thanks for recommending it. However the time complexity is still O(n) in both cases
@atharvarajadhyaksha4813Ай бұрын
@@CodeWithZi True. You still need a loop for actualSum. But for competitive programming it will always be better to avoid having an extra loop.
@Madhava_P7Ай бұрын
set(range(n)-set(given))
@YashGondane-p3fАй бұрын
thank you so much for this cool approach
@CodeWithZiАй бұрын
You're very welcome!
@danielhod53Ай бұрын
I got a bit misleaded because the example array in the beginning is sorted so my first instict was to loop through the array and calculate (array[i+1]-array[i]) and if it's not equal to 1 then the number missing is (array[i+1]+array[i]) / 2 but by seeing the question in LeetCode you can't assume that the array is sorted so it won't work
@CodeWithZiАй бұрын
great approach!
@black_ice7822Ай бұрын
That’s pretty intuitive. Guess you could still do that if you sort the array first
@heathens2867Ай бұрын
Can't we use i==arr[i] and if it's false, then i is the answer
@gnanaharishАй бұрын
Works only if array is sorted , here array is not sorted , so it will not work
@atharvarajadhyaksha4813Ай бұрын
As the other comment said, you'll have the added burden of sorting the array and depending on what sort you use, you may end up with a worst case time complexity of O(n^2)
@abhikannor9301Ай бұрын
Sum of size of total number of array - total sum of array