Given two binary strings s1 and s2. The XOR of them is X, the task is to find the number of ways to swap two-bit positions in string s1 such that XOR formed between new s1 and s2 is not same as X.
Input: s1 = “01011”, s2 = “11001”
swap bits of index(1-based) (1, 4), (2, 3), (3, 4), or (3, 5) such that XOR value is changed.
Input: s1 = “011000”, s2 = “010011”
- Count the number of 1’s and 0’s in s1.
- Traverse in the string s1, and check for two cases:
- 0 and 0 in s1[i] and s2[i], as replacing 0 with 1, will change the XOR value.
- 1 and 0 in s1[i] and s2[i], as replacing 1 with 0 will change the XOR value.
- For the first case, the number of ways of replacement will be the number of ones-already used 1’s.
- For the second case, the number of ways of replacement will be the number of zeros-already used 0’s.
- summation of number of ways in both the cases will be the answer.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Check if bits in range L to R of two numbers are complement of each other or not
- Maximum number of contiguous array elements with same number of set bits
- Find the number of ways to divide number into four parts such that a = c and b = d
- Check if a number has same number of set and unset bits
- Toggle all even bits of a number
- Set all the bits in given range of a number
- For every set bit of a number toggle bits of other
- Check whether the number has only first and last bits set | Set 2
- Swap bits in a given number
- Toggle first and last bits of a number
- Minimum number using set bits of a given number
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Count number of bits to be flipped to convert A to B
- Program to invert bits of a number Efficiently
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.