Class Solution
-
- All Implemented Interfaces:
public final class Solution3226 - Number of Bit Changes to Make Two Integers Equal\.
Easy
You are given two positive integers
nandk.You can choose any bit in the binary representation of
nthat is equal to 1 and change it to 0.Return the number of changes needed to make
nequal tok. If it is impossible, return -1.Example 1:
Input: n = 13, k = 4
Output: 2
Explanation: Initially, the binary representations of
nandkare <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>. We can change the first and fourth bits ofn. The resulting integer is <code>n = (<ins> 0 </ins>10<ins> 0 </ins>)<sub>2</sub> = k</code>.Example 2:
Input: n = 21, k = 21
Output: 0
Explanation:
nandkare already equal, so no changes are needed.Example 3:
Input: n = 14, k = 13
Output: \-1
Explanation: It is not possible to make
nequal tok.Constraints:
<code>1 <= n, k <= 10<sup>6</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerminChanges(Integer n, Integer k)-
-
Method Detail
-
minChanges
final Integer minChanges(Integer n, Integer k)
-
-
-
-