Advertisement
varun1729

Untitled

Feb 18th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.32 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int minOperations(int n) {
  4. if (n==0)
  5. return 0;
  6. int x = floor(1.0*log2(n));
  7. int y = ceil(1.0*log2(n));
  8. if(abs(pow(2,x)-n)>abs(pow(2,y)-n))
  9. return 1+ minOperations((abs(pow(2,y)-n)));
  10. else
  11. return 1+ minOperations((abs(pow(2,x)-n)));
  12. }
  13. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement