Find X to minimize XOR of N and division of N by 2 raised to power X
Last Updated :
21 Oct, 2022
Given a binary string S of a number N, the task is to find a number X such that we can minimize the value of N by changing N = N ??N/2X?, where 1 ? X ? |S| and ? denotes the bitwise XOR operation.
Examples:
Input: S = “110”
Output: 1
?Explanation: Since S = 110 is the binary representation of 6, N = 6.
On choosing X = 1, N becomes 6 ? ?6 / 21? = 5.
We can show that this is the minimum value of N we can achieve
Input: S = ”10”
?Output: 2
Approach: The problem can be solved based on the following idea:
To minimize the value of N, we have to change the most significant bit (MSB) to 0. As X cannot be 0, this is not possible. So the most optimal is to change the second most significant set bit to 0. The set bit will become 0 when we XOR another number having a set bit at that position.
Say, the bit difference between MSB and second most significant set bit is K. So, for the second number N should be right shifted by K that is the same as dividing N by 2K. So optimal X = K. To get X
- Iterate a loop from second character to last character of string and count number of 0’s until 1 occur
- Whatever value we get, add one to get the number X such.
Follow the steps mentioned below to implement the above idea:
- Set count = 1.
- Iterate a for loop from 1 to S.length()-1 and check the ith character of a string such that:
- If ith character of a string is 0 then increment the value of count and whenever 1 occurs then break from the loop.
- After that print the value of count which is the number X such that we can minimize the value of N.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findValue(string s, int len)
{
int count = 1;
for ( int i = 1; i < len; i++) {
if (s[i] == '0' )
count++;
else
break ;
}
return count;
}
int main()
{
string S = "110" ;
int len = S.length();
int X = findValue(S, len);
cout << X;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int findValue(String s, int len)
{
int count = 1 ;
for ( int i = 1 ; i < len; i++) {
if (s.charAt(i) == '0' )
count++;
else
break ;
}
return count;
}
public static void main(String[] args)
{
String S = "110" ;
int len = S.length();
int X = findValue(S, len);
System.out.println(X);
}
}
|
Python3
def findValue(s, length):
count = 1
for i in range ( 1 , length):
if (s[i] = = '0' ):
count = count + 1
else :
break
return count
if __name__ = = '__main__' :
S = "110"
length = len (S)
X = findValue(S, length)
print (X)
|
C#
using System;
public class GFG {
public static int findValue(String s, int len)
{
int count = 1;
for ( int i = 1; i < len; i++) {
if (s[i] == '0' )
count++;
else
break ;
}
return count;
}
static public void Main()
{
String S = "110" ;
int len = S.Length;
int X = findValue(S, len);
Console.WriteLine(X);
}
}
|
Javascript
function findValue(s, len) {
let count = 1;
for (let i = 1; i < len; i++) {
if (s[i] == '0' )
count++;
else
break ;
}
return count;
}
let S = "110" ;
let len = S.length;
let X = findValue(S, len);
document.write(X);
|
Time Complexity: O(|S|) where |S| is the length of the string
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...