algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

main.cpp (1164B)


      1 #include <iostream>
      2 #include <vector>
      3 
      4 //- can we make zeroes?
      5 //    - yes
      6 //        - iteratively select smallest element, matched with one other element and decrement
      7 //        - specifically, yes is true iff the number of non-zero elements >= lowestValue
      8 //    - no
      9 //        - under this case, we know the above conditional is not true
     10 //            - removing elements won't help us so we return 0
     11 
     12 int main () {
     13     int testCases, current, sz = 0;
     14     std::cin >> testCases;
     15 
     16     for(int i = 0; i < testCases; ++i) {
     17         std::cin >> sz;
     18         
     19         std::vector<int> nums {};
     20 
     21         for(int x = 0; x < sz; ++x) {
     22             std::cin >> current;
     23             nums.push_back(current);
     24         }
     25 
     26         int smallest = nums[0];
     27         int nonZero = 0;
     28 
     29         for(int z = 0; z < nums.size(); ++z) {
     30             if(nums[z] < smallest) {
     31                 smallest = nums[z];
     32             }
     33             if(nums[z] > 0) {
     34                 nonZero += 1;
     35             }
     36         }
     37 
     38         if(smallest < nonZero || smallest == 0) {
     39             std::cout << nums.size() << std::endl;
     40         } else {
     41             std::cout << 0 << std::endl;
     42         }
     43     }
     44 }