Boards
InterviewTime limit1sMemory limit128 MB
From the smallest power of two at least K, find the fewest board halvings so some pieces sum to exactly K.
- Level
Medium5 of 10
- Topics
- Bit manipulation, Greedy
- Solved
- No attempts yet
Problem
Captain Pitt wants to build a fence. To do that he needs boards whose lengths add up to exactly .
He has only two things to work with:
- A machine that cuts any single board into two equal halves.
- One starting board whose length is a power of two that he gets to choose (a carpenter friend delivers it).
Each cut takes one board and turns it into two boards of half its length. Beginning from that single power-of-two board, find the minimum number of cuts needed so that some subset of the boards he ends up with has lengths summing to exactly .
Input
The first line contains the number of test cases ().
Each of the next lines contains one integer (): the total board length Captain Pitt needs in that test case.
Output
For each test case, print a single integer on its own line: the minimum number of cuts Captain Pitt has to make.