From 7aabbbd13ff3601a9048a87ad6e488170e6f97bd Mon Sep 17 00:00:00 2001 From: Kaushal Date: Sat, 3 Nov 2018 16:36:17 +0530 Subject: [PATCH 1/2] :clock11: GS IITD 2018 --- minbin.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 minbin.cpp diff --git a/minbin.cpp b/minbin.cpp new file mode 100644 index 0000000..0fa4f1c --- /dev/null +++ b/minbin.cpp @@ -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 a) { + int n = a.size(); + vector 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; +} \ No newline at end of file From 367e6b026eba17fabf9b087599da1436f005bfbc Mon Sep 17 00:00:00 2001 From: DEEPAMJAIN Date: Sat, 3 Nov 2018 17:43:32 +0530 Subject: [PATCH 2/2] Update minbin.cpp --- minbin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minbin.cpp b/minbin.cpp index 0fa4f1c..98233d1 100644 --- a/minbin.cpp +++ b/minbin.cpp @@ -12,7 +12,7 @@ complete array. O(N + log N) */ int minBin(vector a) { - int n = a.size(); + int n = max_element(a.begin(),a.end()); vector c(n + 30); for (int ai : a) c[ai]++; @@ -22,4 +22,4 @@ int minBin(vector a) { c[i + 1] += c[i] >> 1; } return ans; -} \ No newline at end of file +}