Command Palette
Search for a command to run...
Gold Coin Rooms
Prefix Sum
Gold Coin Rooms
HardPrefix SumHash MapSubarray Sum
A house has N rooms numbered 1 to N. Each room contains a specific number of gold coins.
Given a target value K, find a continuous sequence of rooms where the total sum of gold coins is exactly equal to K.
If multiple such sequences exist, return the one that starts with the smallest room number.
Test Case
Input: N = 10 K = 15
Coins: 5 3 7 14 18 1 18 4 8 3
Output: 1 3
Explanation:
The sum of coins in rooms 1 to 3 → 5 + 3 + 7 = 15
The sum of coins in rooms 8 to 10 → 4 + 8 + 3 = 15
Both sequences give the required sum, but room 1 is smaller than room 8, so the answer is 1 3.
Examples
Example 1
Input:
10, 15, [5, 3, 7, 14, 18, 1, 18, 4, 8, 3]Output:
[1, 3]Example 2
Input:
5, 10, [1, 2, 3, 4, 5]Output:
[1, 4]Loading...
10, 15, [5, 3, 7, 14, 18, 1, 18, 4, 8, 3]
[1, 3]