Company: Factset

Difficulty: medium

Problem Statement

Equalizing Box Piles A warehouse crew has stacked crates into n columns of differing heights. In a single move, the crew may pull any number of crates off the tallest column so that its height drops to match the column that is currently the second tallest. Work out the fewest such moves needed before every column stands at the same height. Function Description Complete the function pilesOfBoxes which has the following parameter: vector<int> boxesInPiles : each boxesInPiles[i] gives the starting height of one column Return long: the fewest moves needed Constraints 1 ≤ n ≤ 2 × 10⁵ 1 ≤ boxesInPiles[i] ≤ 2 × 10⁶ Example 1 Sample Input n = 3 boxesInPiles = [5, 2, 1] Sample Output 3 Explanation First, take 3 crates off boxesInPiles[0], leaving boxesInPiles = [2, 2, 1]; that is one move. The two columns now at height 2 each still need to drop by 1 crate apiece to reach height 1, and each of those is a separate move since a single move touches only one column. Counting all of them gives

More Factset OA questionsInterview experiences