diff --git a/6_find_all_missing_numbers.cpp b/6_find_all_missing_numbers.cpp new file mode 100644 index 0000000..a3e44de --- /dev/null +++ b/6_find_all_missing_numbers.cpp @@ -0,0 +1,33 @@ +/* Problem: + * https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ + */ + +#include +#include +#include + +std::ostream &operator<<(std::ostream &out, const std::vector &&nums) { + out << "{"; + for (const int num : nums) + out << num << ", "; + out << "}"; + return out; +} + +std::vector solution(const std::vector &input) { + std::vector num_exist(input.size(), false); + std::vector missing_nums; + for (const int num : input) + num_exist[num - 1] = true; + for (int i = 0, upto = input.size(); i < upto; i++) { + if (!num_exist[i]) + missing_nums.push_back(i + 1); + } + return missing_nums; +} + +int main() { + std::cout << solution({4, 3, 2, 7, 8, 2, 3, 1}) << '\n' + << solution({1, 1}) << '\n'; + return 0; +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c7fde9..bffcb39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,3 +12,5 @@ add_executable(1_longest_substring_with_k_distinct_characters 1_longest_substrin add_executable(2_num_of_islands 2_num_of_islands.cpp) add_executable(3_squares_of_a_sorted_array 3_squares_of_a_sorted_array.cpp) + +add_executable(6_find_all_missing_numbers 6_find_all_missing_numbers.cpp)