1341. Minimum Number of Flips to Make OR of Array K

MediumBit ManipulationBit ManipulationGreedyOR

Given an array of non-negative integers and a target k, you may flip any single bit of any element (0 to 1 or 1 to 0). Return the minimum number of bit flips needed so that the bitwise OR of all elements equals exactly k.

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

Output: Return the minimum number of individual bit flips.

Examples

Example 1
Input: {"nums":[1,2,4],"k":7}
Output: 0
Explanation: The OR is already 7, so no flips are needed.
Example 2
Input: {"nums":[7,7],"k":0}
Output: 6
Explanation: All six set bits must be cleared -> 6.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →