fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int minOperationsToDestroyArray(vector<int>& arr) {
  9. unordered_map<int, int> freqMap;
  10. for (int num : arr) {
  11. freqMap[num]++;
  12. }
  13.  
  14. int minOperations = 0;
  15. for (auto& entry : freqMap) {
  16. int freq = entry.second;
  17. if (freq == 1) {
  18. return -1; // According to Tariquddin’s law
  19. } else {
  20. // According to Tanmai’s Law
  21. minOperations += freq / 3 + (freq % 3 != 0);
  22. }
  23. }
  24. return minOperations;
  25. }
  26.  
  27. int main() {
  28. vector<int> arr = {1, 5, 5, 1, 1, 8, 8, 10, 10};
  29. int minOps = minOperationsToDestroyArray(arr);
  30. if (minOps == -1) {
  31. cout << "It's not possible to destroy the array" << endl;
  32. } else {
  33. cout << "Minimum number of operations to destroy the array-> " << minOps << endl;
  34. }
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Minimum number of operations to destroy the array-> 4