Open In App

Find X to minimize XOR of N and division of N by 2 raised to power X

Last Updated : 21 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the value of X
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;
}
 
// Driver Code
int main()
{
    string S = "110";
    int len = S.length();
   
    // Function call
    int X = findValue(S, len);
    cout << X;
    return 0;
}
 
// This code is contributed by aarohirai2616.


Java




// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the value of X
    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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String S = "110";
        int len = S.length();
 
        // Function call
        int X = findValue(S, len);
        System.out.println(X);
    }
}


Python3




# Python code to implement the approach
 
# Function to find the value of X
def findValue(s, length):
 
    count = 1
    for i in range(1, length):
        if (s[i] == '0'):
            count = count+1
        else:
            break
 
    return count
 
# Driver Code
if __name__ == '__main__':
    S = "110"
    length = len(S)
     
    # Function call
    X = findValue(S, length)
    print(X)
 
    # This is contributed by aarohirai2616.


C#




// C# code to implement the approach
using System;
public class GFG {
 
  // Function to find the value of X
  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()
  {
 
    // Code
    String S = "110";
    int len = S.Length;
 
    // Function call
    int X = findValue(S, len);
    Console.WriteLine(X);
  }
}
 
// This code is contributed by lokeshmvs21.


Javascript




// Javascript code to implement the approach
 
// Function to find the value of X
function findValue(s, len) {
    let count = 1;
    for (let i = 1; i < len; i++) {
        if (s[i] == '0')
            count++;
        else
            break;
    }
    return count;
}
 
// Driver code
let S = "110";
let len = S.length;
 
// Function call
let X = findValue(S, len);
document.write(X);
 
// This code is contributed by saurabh_jaiswal.


Output

1

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

Similar Reads