Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions minbin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Goldman Sachs IITD 2018

We are given an array of size N, we can delete a subset b1b2b3...bk from the
array if 2^b1 + 2^b2 + …..2^bk = 2^x for non-negative integer x where ^ is the
power operator. Find the minimum number of steps required to delete the
complete array.

0 <= ai <= 1000000
1 <= N <= 1000000

O(N + log N)
*/
int minBin(vector<int> a) {
int n = max_element(a.begin(),a.end());
vector<int> c(n + 30);
for (int ai : a) c[ai]++;

int ans = 0;
for (int i = 0; i < n + 25; i++) {
if (c[i] & 1) ans++;
c[i + 1] += c[i] >> 1;
}
return ans;
}