Education + Jobs Hiring Website - 2025
0 like 0 dislike
236 views

ago in Online Assessments by Expert (139,750 points) | 236 views

1 Answer

0 like 0 dislike
#include <iostream>
#include <vector>
#include <map>

using namespace std;

int minOperations(vector<int>& arr) {
    map<int, long long> freq;
    
    for (int x : arr) {
        freq[x]++;
    }
    
    int operations = 0;
    
    for (auto it = freq.begin(); it != freq.end(); ++it) {
        int exp = it->first;
        long long count = it->second;
        
        if (count >= 2) {
            freq[exp + 1] += count / 2;
        }
        if (count % 2 != 0) {
            operations++;
        }
    }
    
    return operations;
}
ago by (140 points)