O(n) time and O(1) space Java Solution. FInding sum and then subtracting


  • 3
    C
    public int missingNumber(int[] nums) {
        int len =nums.length;
        int sum = (int)((len*(len+1))/2); //Finding sum of all the integers from 1 to n 
        for(int i: nums)
            sum-=i;
        return sum;
    }

  • 0
    X

    One potential problem would be if the sum is too large?


Log in to reply
 

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