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
- 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
- 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
- Swap bits in a given number
- Check whether the number has only first and last bits set | Set 2
- For every set bit of a number toggle bits of other
- Toggle first and last bits of a number
- Toggle all even bits of a number
- Set all the bits in given range of a number
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.