Skip to content

Commit f1aa3d8

Browse files
committed
Missing number array code
1 parent 05d514a commit f1aa3d8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

MissingNumberArray.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <numeric>
3+
#include <vector>
4+
5+
int sumOfElements(const std::vector<int> series) {
6+
return std::accumulate(series.begin(), series.end(), 0);
7+
}
8+
9+
int sumOfElements(const int n) {
10+
return (n * (1 + n)) / 2;
11+
}
12+
13+
int find_missing(const std::vector<int>& input) {
14+
int expectedSum = sumOfElements(input);
15+
int realSum = sumOfElements(input.size() + 1);
16+
17+
return realSum - expectedSum;
18+
}

0 commit comments

Comments
 (0)