1362. Count Bit Changes to Sort Array

MediumBit ManipulationBit ManipulationHammingSorting

Sort the given array in non-decreasing order, then compare it position by position with the original array. Return the total number of bit positions that differ, summed over all positions (the sum of Hamming distances between each original element and the element that ends up in its place).

Input: A JSON object {"nums": [<non-negative integers>]}.

Output: Return the total number of differing bits across all positions.

Examples

Example 1
Input: {"nums":[3,1,2]}
Output: 4
Explanation: Comparing against the sorted array gives 4 differing bits.
Example 2
Input: {"nums":[1,2,3]}
Output: 0
Explanation: Already sorted, so nothing differs -> 0.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →