Skip to content

Minimize Length of Array Using Operations⚓︎

Link

Solution⚓︎

See reference (Chinese) and std::count.

class Solution {
public:
    int minimumArrayLength(vector<int>& nums) {
        int m = *min_element(nums.begin(), nums.end());
        for (int num : nums) {
            if (num % m) return 1;
        }
        return (count(nums.begin(), nums.end(), m) + 1) / 2;
    }
};
  • Time complexity: \(O(n)\);
  • Space complexity: \(O(1)\).