268_Missing Number
[Easy][Array, Math, Bit Manipulation]
Given an array containing n distinct numbers taken from0, 1, 2, ..., n
, find the one that is missing from the array.
Example 1:
Example 2:
Solution 1:
Idea:
Sort the array in ascending order.
Loop over each element, find which element doesn't equal to it's index.
Time Complexity:
Space Complexity:
Solution 2:
Idea:
Use math summation formula.
If there is no element missing, adding from 0 to n gives .
Loop over each element in the array to get the summation. The difference between the expected summation and real summation will be the element missing.
Time Complexity:
Space Complexity:
Last updated