Max Game

3.8

4 votes
Basic Math, Basic Programming, Basics of bit manipulation, Bit Manipulation, Basics of Bit Manipulation
Problem

You are given an integer N.

We represent the number N as the sum of distinct numbers that are powers of 2.

Task

If we put these numbers in an array, your task is to find the maximum element in this array.

Example

Assumption:

N=10

Approach:

10 can be expressed as the sum of [1,2,8] which are powers of 2 and the
maximum of these numbers is 8. Hence, the answer is 8.

Function description

Complete the function solve provided in the editor. This function takes the following one parameter and returns the required answer:

N: the integer which is to be represented as the sum of powers of 2.

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

The first line contains T denoting the number of test cases. T also specifies the number of times you have to run the solve function on a different set of inputs.
For each test case:
The first line contains the integer N.

Output format
For each test case, print the answer in a new line. 

Constraints

1T10
1N106

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Sample Input
2
4
11
Sample Output
4
8
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The first line contains the number of test cases.

In the first test case, 4 is itself a power of 2 hence it is the answer.

In the second test case, 11 can be represented as the sum of [2,8,1] which are all powers of 2. The maximum among these is 8, So 8 is the answer.

 

Editor Image

?