Company: Titan

Difficulty: medium

Problem Statement

You have a bag of size n and m boxes. The size of the i -th box is a[i] , and every a[i] is a non-negative power of two (that is, a[i] = 2^k for some integer k >= 0 ). You may repeat the following operation any number of times: Pick a box of size s with s > 1 and replace it by two boxes of size s / 2 each. This counts as one division . Afterwards you choose some of the boxes you own (you do not have to use all of them) and put them into the bag. The bag is completely filled if the sizes of the chosen boxes add up to exactly n . Find the minimum number of divisions needed so that some subset of the resulting boxes sums to exactly n . If it is impossible no matter how you divide, print -1 . You must answer several independent test cases. Boxes are not shared between test cases. Input Format The first line contains a single integer t — the number of test cases. For each test case: The first line contains two space-separated integers n and m — the size of the bag and the number of bo

More Titan OA questionsInterview experiences