- Rust already provided a very helpful method
count_onesto count number of 1's in the binary representation of an integer. - Therefore I used this together with
sort_unstable_byto implement the sort
Here's my solution
count_ones to count number of 1's in the binary representation of an integer.sort_unstable_by to implement the sortHere's my solution
parseInt(str, 2) to convert the binary string to an integerHere's my solution:
How I Solved It
n was greater than 0, I calculated the remainder rem when n is divided by 2, and then I set n to the value of n / 2 (integer division).rem from the previous cycle as prev_rem, and in each cycle, I check if new rem equals prev_remfalse early. Otherwise I proceed to the next cyclen becomes 0, it means there wasn't consecutive equal remainders and so I return trueturned_onturned_on, it was a valid permutation and I added it to a result I had createdtotal which will store our computed decimal numbercount that starts from 31 and goes down to 0 for each loop cyclen is greater than 0, I find the remainder after it's divided by 2. If this remainder is 1, I add to total, the value of 2 to the power of countcarry variable used to track remainders from bit additions, initially 0res variable, which is a string to track the result of the additioncarryresHere's the code
nums[j] / nums[i] <= knums gives the minimum number of removals to arrive at itHere's my solution:
result array of same length as numsnumsi, I calculate the new target_indexi + steps, then I add the length of nums to it, so that it'll ensure the value is positivei + steps was positive in the first place, then adding nums will push it out of bounds, so I also do a % nums to the answerresult[i] to the value at target_index of nums