diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-11 23:20:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-11 23:20:09 +0000 |
| commit | 993b5cfec02444f8ab158507ea64f42bcd3d175b (patch) | |
| tree | 3678bcde9af7c2fb9a2fcb9f8cd5deefd17f59c2 /challenge-201/paulo-custodio/cpp/ch-1.cpp | |
| parent | 51465a4186e14e5f9ae26e7f5b20dbb9f5ea8aae (diff) | |
| parent | 0ad64a5e5a740655432a36b2af0c35760289cf89 (diff) | |
| download | perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.gz perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.bz2 perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.zip | |
Merge pull request #7709 from pauloscustodio/master
Add Perl, C, C++ and Forth solutions
Diffstat (limited to 'challenge-201/paulo-custodio/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-201/paulo-custodio/cpp/ch-1.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-201/paulo-custodio/cpp/ch-1.cpp b/challenge-201/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..88557d44f7 --- /dev/null +++ b/challenge-201/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,53 @@ +/* +Challenge 201 + +Task 1: Missing Numbers +Submitted by: Mohammad S Anwar + +You are given an array of unique numbers. + +Write a script to find out all missing numbers in the range 0..$n where $n +is the array size. + +Example 1 + +Input: @array = (0,1,3) +Output: 2 + +The array size i.e. total element count is 3, so the range is 0..3. +The missing number is 2 in the given array. + +Example 2 + +Input: @array = (0,1) +Output: 2 + +The array size is 2, therefore the range is 0..2. +The missing number is 2. +*/ + +#include <algorithm> +#include <iostream> +#include <vector> + +void print_missing(std::vector<int>& nums) { + std::sort(nums.begin(), nums.end()); + for (size_t i = 0; i <= nums.size(); i++) { // check 0..N + if (!std::binary_search(nums.begin(), nums.end(), i)) + std::cout << i << " "; + } + std::cout << std::endl; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr << "usage: ch-1 nums..." << std::endl; + exit(EXIT_FAILURE); + } + + std::vector<int> nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + print_missing(nums); +} |
