Created
November 3, 2024 05:43
-
-
Save sAVItar02/55c06023d0e4ce7544ea4fae0d09b305 to your computer and use it in GitHub Desktop.
Missing number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var missingNumber = function(nums) { | |
let sum = ((nums.length) * (nums.length + 1)) / 2; | |
let sum2 = 0; | |
for(let i=0; i<nums.length; i++) { | |
sum2 += nums[i]; | |
} | |
return sum - sum2; | |
}; | |
// Find the sum of all numbers in the range by the formula (n*(n+1)) / 2 | |
// Find the sum of all numbers in the array | |
// Subtract both sums to get the answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment