diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-30 13:49:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-30 13:49:48 +0100 |
| commit | c459362b55a9a174f13dc1fd29c9c651ed738e6d (patch) | |
| tree | 3ee4b6918c0fa5de3cd206e5afbc48560a7c740d /challenge-080/ash/cpp/ch-1.cpp | |
| parent | 96f3ffb9600ca187d982c537939c096b084fe409 (diff) | |
| parent | f3610ba736a27f0c3373bf65b424d3cbba85d2df (diff) | |
| download | perlweeklychallenge-club-c459362b55a9a174f13dc1fd29c9c651ed738e6d.tar.gz perlweeklychallenge-club-c459362b55a9a174f13dc1fd29c9c651ed738e6d.tar.bz2 perlweeklychallenge-club-c459362b55a9a174f13dc1fd29c9c651ed738e6d.zip | |
Merge pull request #2416 from ash/master
Solutions 080-1 in C++ and Raku
Diffstat (limited to 'challenge-080/ash/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-080/ash/cpp/ch-1.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-080/ash/cpp/ch-1.cpp b/challenge-080/ash/cpp/ch-1.cpp new file mode 100644 index 0000000000..278833993b --- /dev/null +++ b/challenge-080/ash/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +/* + Task 1 from + https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +*/ + +#include <iostream> +#include <vector> +#include <map> + +using namespace std; + +int missing_n(vector<int> n) { + map<int, bool> v; + + int max = n[0]; + for (auto x : n) { + v[x] = true; + if (x > max) max = x; + } + + for (auto c = 1; c < max; c++) { + if (!v[c]) { + return c; + break; + } + } + + return max + 1; +} + +int main() { + cout << (missing_n(vector<int> {5, 2, -2, 0}) == 1) << endl; + cout << (missing_n(vector<int> {1, 8, -1}) == 2) << endl; + cout << (missing_n(vector<int> {5, 2, -2, 0}) == 1) << endl; + + cout << (missing_n(vector<int> {5, 2, 3, 1, -2, 0}) == 4) << endl; + cout << (missing_n(vector<int> {5, 2, 3, 1, 4, -2, 0}) == 6) << endl; +} |
