aboutsummaryrefslogtreecommitdiff
path: root/missing_number.cpp
blob: 03dd331af5b37463eee5500852fdd264f6b85de9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "leetcode.h"

class Solution {
public:
  int missingNumber(vector<int> &nums) {
    int sum = 0, n = nums.size();
    int total = n * (n + 1) / 2;
    for (auto number : nums)
      sum += number;
    return total - sum;
  }
};

int main() {
  Solution obj;
  vector<int> nums = {9, 6, 4, 2, 3, 5, 7, 0, 1};
  cout << obj.missingNumber(nums);
}