Suppose we have a sequence( 1 2 4 8 16 32 64 ....... ) . And we have a number we have to tell where this number lies with respect to the sequence. CHECK DESCRIPTION FOR BETTER UNDERSTANDING

We have a sequence 1 2 4 8 16 32 64 128 …
If a number is given we have to tell its location with respect to the sequence.
[1] If the number given is same as one of the number in the sequence then we have to print the given number .
FOR EXAMPLE :- If the given no is 8 then we will print 8 as 8 is one of the number in the sequence. For 64 we will print 64.

[2] else If the number given is just more then one of the number of the sequence then we will print that no of the sequence.
FOR EXAMPLE :- if the number is 6 then we see that 6 is just more than the no 4 of the sequence than we will print 4. For 31 we will print 16.

HELP ME TO UNDERSTAND A LOGIC FOR THIS PROBLEM .

pow(2,floor(log2(n) ) )

1 Like

Just do binary search and find floor of the given element.

1 Like

Initialize ans = 1
Multiply ans by 2, till it is <= num
Divide ans by 2. (Because when we multiplied it, it gave us a larger number)
Print ans.

P.S: The solution given by @ritik_sharma is also correct, but after a number, it prints all the values in scientific notation.

1 Like

int(math.log(number,2))**2

1 Like

thanks a lot .

1 Like

This is very great way !!!
Thanks a lot