Skip to content

Latest commit

 

History

History
50 lines (37 loc) · 1.27 KB

File metadata and controls

50 lines (37 loc) · 1.27 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Completed during Weekly Contest 407 (q1)

Back to top


First completed : July 21, 2024

Last updated : July 21, 2024


Related Topics : Bit Manipulation

Acceptance Rate : 63.64 %


Notes

I know there's probably an O(1) bit manip solution to this but given the time constraints of this contest, I htought it'd be more productive for me to just type out the "brute force" ish solution to get the contest going.


Solutions

Python

class Solution:
    def minChanges(self, n: int, k: int) -> int:
        counter = 0
        while n > 0 :
            if n % 2 == 0 and k % 2 == 1 :
                return -1
            elif n % 2 == 1 and k % 2 == 0 :
                counter += 1
            n //= 2
            k //= 2
        return -1 if k else counter