Skip to content

Instantly share code, notes, and snippets.

@SrinSS01
Created May 21, 2022 17:02
Show Gist options
  • Save SrinSS01/821ccd2b0df2aa831c3bbc4149f90a00 to your computer and use it in GitHub Desktop.
Save SrinSS01/821ccd2b0df2aa831c3bbc4149f90a00 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N;
int max = 0, sum = 0;
int count = 0;
cin >> N;
int A[N];
for (int& arrayElement: A) {
cin >> arrayElement;
sum += arrayElement;
if (max < arrayElement) max = arrayElement;
}
while (sum != 0) {
int X = int(pow(2, log(max) / log(2)));
max = 0;
for (int& arrayElement: A) {
if (arrayElement == 0) continue;
int z = arrayElement - X;
if (z >= 0) {
arrayElement = z;
sum -= X;
}
if (max < arrayElement) max = arrayElement;
}
count++;
}
cout << count << endl;
/* int i = 0;
while (N--) {
int p = 1;
if(A[i] != 0) {
while(A[i] > 0) {
A[i] = A[i] - int(pow(2,p));
p--;
if(A[i] < 0) {
while(A[i] < 0) {
A[i] = A[i] + int(pow(2,p));
}
}
}
}
i++;
}*/
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment