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)
- Number of ways to change the Array such that largest element is LCM of array
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Number of ways to split N as sum of K numbers from the given range
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Smallest number possible by swapping adjacent even odd pairs
- Count of numbers whose 0th and Nth bits are set
- 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
- Check if a number has same number of set and unset bits
- Find the number of ways to divide number into four parts such that a = c and b = d
- Toggle first and last bits of a number
- Set all the bits in given range of a number
- Toggle all even bits of a number
- For every set bit of a number toggle bits of other
- Swap bits in a given number
- Check whether the number has only first and last bits set | Set 2
- Minimum number using set bits of a given number
- Count number of bits to be flipped to convert A to B
- Count number of bits changed after adding 1 to given N
- 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.