Skip to content

Add C++ solution for Minimum Bit Flips to Convert a Number (Bit Manipulation)#411

Open
maitriupadhyay03-cell wants to merge 2 commits into
saloni-jaiswal-dev:mainfrom
maitriupadhyay03-cell:feat/add-minimum-bit-flips-solution
Open

Add C++ solution for Minimum Bit Flips to Convert a Number (Bit Manipulation)#411
maitriupadhyay03-cell wants to merge 2 commits into
saloni-jaiswal-dev:mainfrom
maitriupadhyay03-cell:feat/add-minimum-bit-flips-solution

Conversation

@maitriupadhyay03-cell

Copy link
Copy Markdown

Description

This PR adds a C++ solution for the Minimum Bit Flips to Convert a Number problem using bit manipulation.

Closes #320

Approach

  • XOR the two numbers (start ^ goal) to get a number where each set bit (1) represents a position where the two numbers differ.
    • Count the set bits in the XOR result using __builtin_popcount() — each set bit is one required flip.

Complexity

  • Time: O(log n)
    • Space: O(1)

Test Cases

start goal Expected Output
10 7 3
3 4 3
0 0 0
7 7 0
1 0 1

Implemented a function to find the longest common prefix among an array of strings using horizontal scanning. Added test cases to validate the functionality.
Implement a solution to calculate the minimum number of bit flips needed to convert one integer to another using XOR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minimum bit flips to convert a number (bit manipulation)

1 participant