Created
May 21, 2022 17:02
-
-
Save SrinSS01/821ccd2b0df2aa831c3bbc4149f90a00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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