O(n) time and O(1) space, C solution

  • 2

    We can find the missing number by subtracting sum of first n positive integer (sumAll) with sum of all number in nums (sumNum). sumAll can be easily computed using Triangular Number.

    int missingNumber(int* nums, int numsSize) {
        int sumAll = (numsSize  * (numsSize + 1)) / 2;
        int sumNum = 0;
        for (int i = 0; i < numsSize; i++) {
            sumNum += nums[i];
        return (sumAll - sumNum);

  • 0

    Nice solutions!

Log in to reply

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