O(n) and O(1) solution in C++


  • 0
    T

    class Solution {
    public:
    int missingNumber(vector<int>& nums) {
    long sum = 0;
    long n = nums.size();
    for(auto &i: nums)
    {
    sum+=i;
    }

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

    };


Log in to reply
 

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