Simple implementation using mathematics


  • 0
    T
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int sumBig=0;
            int sumSmall = 0;
            for(int i=0; i<nums.size(); i++)
            {
                sumBig = sumBig + i + 1;
                sumSmall = sumSmall + nums[i];
            }
            
            return (sumBig - sumSmall);
        }
    };

Log in to reply
 

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