Worms Problem

Worms Problem — ExecCode Medium DSA Practice

Solve the Worms problem on ExecCode. Free online medium DSA practice in Searching & Sorting. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Worms are packed into piles in order. Pile i contains piles[i] worms. Worms are numbered from 1 through the total, filling pile 1 first, then pile 2, and so on. For each label, return the 1-based pile that contains that worm. You are given: piles — worms in each pile labels — worm numbers to locate Return the 1-based pile index of each label. Build the prefix sums of the pile sizes. The pile of a worm is the first prefix that is at least that worm number.

Examples

Input {'piles': [2, 7, 3, 4, 9], 'labels': [1, 25, 11]}; Output [1, 5, 3]. Input {'piles': [5], 'labels': [1, 5]}; Output [1, 1]. Input {'piles': [1, 1, 1], 'labels': [2, 3]}; Output [2, 3]

Constraints

1 <= piles.length <= 100000 1 <= piles[i] <= 1000 1 <= labels.length <= 100000 Each label is between 1 and the total number of worms

Practice Worms free on ExecCode. Browse DSA problems, topic map, and placement guides.