Given two binary numbers strings and of length . Find the number of ways of swapping two bits in s1(only s1 not s2) so that bit-wise OR of these two numbers s1 and s2 are changed.
Note: The length of both string must be equal, you can take leading zeros in case of different length.
Input: s1 = "01011", s2 = "11001" Output: 4 Explanation: You can swap the bit of s1 at indexed: (1, 4), (2, 3), (3, 4) and (3, 5) there are 4 ways possible. Input: s1 = "011000", s2 = "010011" Output: 6
Approach: Initialize a variable result as zero that stores number of ways to swap bits of s1 so that bitwise OR of s1 and s2 changes. Initialize four variables say , , , and as zero.
Traverse both strings from left side and check for the below conditions to increment values of the
variable declared above:
- If the current bit of both s1 and s2 are zero, increment c.
- If current bit of s2 is zero and s1 is one, increment d.
- If current bit of s2 is one and s1 is zero, increment a.
- If current bit of both s1 and s2 is one, increment b.
Update the result by (a*d) + (b*c) + (c*d) and return the result.
Below is the implementation of the above approach:
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Minimum bit changes in Binary Circular array to reach a index
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Bit manipulation | Swap Endianness of a number
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Count of 1-bit and 2-bit characters in the given binary string
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum changes required to make two arrays identical
- Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
- Count number of common elements between two arrays by using Bitset and Bitwise operation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.