Open In App

Maximum steps to transform 0 to X with bitwise AND

Improve
Improve
Like Article
Like
Save
Share
Report

For an integer N, there are elements ranging from 0 to N-1. Certain elements can be transformed into other elements. Each transformation requires a certain effort which is equal to 1 unit, for each transformation. An element A can be transformed to element B, if and only if A != B and A & B = A (where & is the bitwise AND operator). We need to find the maximum effort possible to obtain the element with value X,  from the element with value 0, by a series of transformations.
Examples : 
 

Input : X = 2 
Output : 1 
The only way of obtaining 2 is to directly transform 0 to 2 (bitwise AND of 0 and 2 is 0) and hence requires one step. 
Input : X = 3 
Output : 2 
3 can be obtained in two steps. First, transform 0 to 1 (bitwise AND of 0 and 1 is 0). Then, transform 1 to 3 (bitwise AND of 1 and 3 is 1).

The simple solution is to count the number of set bits in X. 
Explanation: 
First, consider a single step transformation from A to B. All the set bits (bits which are equal to 1) of A should be set in B, otherwise bitwise AND of A and B will not be equal to A. If there is any bit that is set in A but not in B, then the transformation is not possible. The unset bits of A can either be set or unset in B, it does not matter. We can then change A to B by setting all the unset bits in A in one single step. Consequently, if we had to transform 0 to X in the least steps, the answer would have been one because bitwise AND of 0 with any number is 0. 
But we have to compute the maximum steps. So in each step, we set each bit starting from the right and a set bit can not be cleared once it set. 
Example: 
Suppose we want to obtain 13 (1101 in binary) from 0. We start by setting the 1st bit from the right by transforming 0 to 1 (0001 in binary). We next set the 3rd bit from the right to form 5 (0101 in binary). The last step would be to set the 4th bit and obtain 13 (1101).

Step to solve this problem:

1.declare an unsigned integer count=0.

2.while n is not equal to zero:

           *update count to count + n&1.

           *n>>=1.

3.return count.
 

C++




// CPP code to find the maximum possible
// effort
#include <bits/stdc++.h>
using namespace std;
 
// Function to get no of set bits in binary
// representation of positive integer n
unsigned int countSetBits(unsigned int n)
{
    unsigned int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
 
// Driver code
int main()
{
    int i = 3;
    cout << countSetBits(i);
    return 0;
}


Java




// Java code to find the maximum
// possible effort
 
class GFG {
     
// Function to get no. of
// set bits in binary
// representation of
// positive integer n
static int countSetBits(int n)
{
    int count = 0;
    while (n != 0)
    {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int i = 3;
    System.out.print(countSetBits(i));
}
}
 
// This code is contributed by Smitha.


Python3




# Python3 code to find the
# maximum possible effort
   
# Function to get no of
# set bits in binary
# representation of positive
# integer n
def countSetBits(n) :
    count = 0
    while (n) :
        count += n & 1
        n >>= 1
    return count
   
# Driver code
i = 3
print (countSetBits(i))
   
# This code is contributed by
# Manish Shaw(manishshaw1)


C#




// C# code to find the maximum
// possible effort
using System;
 
class GFG {
     
// Function to get no. of
// set bits in binary
// representation of
// positive integer n
static int countSetBits(int n)
{
    int count = 0;
    while (n != 0)
    {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    int i = 3;
    Console.Write(countSetBits(i));
}
}
 
// This code is contributed by Smitha.


PHP




<?php
// PHP code to find the
// maximum possible effort
 
// Function to get no of
// set bits in binary
// representation of positive
// integer n
function countSetBits($n)
{
    $count = 0;
    while ($n)
    {
        $count += $n & 1;
        $n >>= 1;
    }
    return $count;
}
 
// Driver code
$i = 3;
echo (countSetBits($i));
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>


Javascript




<script>
  
// JavaScript code to find the maximum possible
// effort
 
// Function to get no of set bits in binary
// representation of positive integer n
function countSetBits(n)
{
    var count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
 
// Driver code
var i = 3;
document.write( countSetBits(i));
 
</script>


Output:  

2

Time Complexity: O(log10N)

Auxiliary Space: O(1)
 



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads