diff --git a/cpp/minimum-number-of-operations-to-make-array-empty.cpp b/cpp/minimum-number-of-operations-to-make-array-empty.cpp new file mode 100644 index 0000000..7c97adf --- /dev/null +++ b/cpp/minimum-number-of-operations-to-make-array-empty.cpp @@ -0,0 +1,32 @@ +#include +#include + +class Solution { + public: + int minOperations(const std::vector &nums) { + std::map freqs; + for (const auto &x : nums) { + ++freqs[x]; + } + + int operations = 0; + + for (const auto &[_, count] : freqs) { + if (count == 1) { + return -1; + } + + switch (count % 3) { + case 0: + operations += count / 3; + break; + case 1: + case 2: + operations += count / 3 + 1; + break; + } + } + + return operations; + } +};