Constant space, Linear Time Java solution

  • 0
     public int missingNumber(int[] nums) {  
       int total = 0;
       int n = nums.length;
       int sum = (n * (n + 1)) / 2; //Formula for the sum of all Natural Numbers up to N
       for(int i : nums) total += i; //total of elements in the array
       return sum - total;  //The difference of the sum and the total is the missing number

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.