Given an array containingndistinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array.

class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        int sum = 0;
        for(int num: nums)
            sum += num;

        return n * (n + 1) / 2 - sum;
    }
}

results matching ""

    No results matching ""