Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minOperations(int n) {
- int c=0, a, b;
- while(n) {
- c++;
- a = pow(2, ceil(log(n)/log(2)));
- b = pow(2, floor(log(n)/log(2)));
- if(a-n < n-b) n = a-n;
- else n = n-b;
- }
- return c;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment