Advertisement
2749. Minimum Operations to Make the Integer Zero
UnknownView on LeetCode
Time: O(log n)
Space: O(1)
Approach
Try k operations; need k <= popcount(num1 - k*num2) and k >= popcount.
2749.cs
C#
// Approach: Try k operations; need k <= popcount(num1 - k*num2) and k >= popcount.
// Time: O(log n) Space: O(1)
public class Solution
{
public int MakeTheIntegerZero(int num1, int num2)
{
if (num1 == 0)
return 0;
for (long op = 1; op < 36; op++)
{
long req = (long)num1 - op * num2;
if (countSetBits(req) <= op && op <= req)
return (int)op;
}
return -1;
}
int countSetBits(long x)
{
int res = 0;
for (int i = 0; i < 64; i++)
{
if (((1L << i) & x) != 0)
res++;
}
return res;
}
}Advertisement
Was this solution helpful?