Leetcode 268 Solution

This article provides solution to leetcode question 268 (missing-number)

https://leetcode.com/problems/missing-number

Solution

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n = nums.size() + 1;
        int total = 0;

        for (int i = 0; i < nums.size(); i++)
            total += nums[i];

        return (n * (n - 1) / 2) - total;
    }
};