BudiBadu Logo
00:00

Minimum Prefix Index Reaching Target

Binary Search Easy 1 views

You are given a list of positive integers ordered from left to right and a target value. As you move through the list, imagine maintaining a running total that grows by adding each number one by one. Your task is to determine the smallest index at which this running total becomes greater than or equal to the target. If you reach the end of the list and the running total never reaches the target, then there is no valid position and the correct answer is -1.

Think of each element as a step that raises a cumulative counter. At the beginning, the counter is zero. With every step, the counter increases. The moment the counter meets or exceeds the target, the position where this happens is the answer you should return. Because all values are positive, the running total increases steadily, and once it has crossed the target, it will never dip below it. If the target is less than or equal to the first element, then the answer is index 0, since the running total reaches the requirement immediately. If the target is larger than the sum of all elements, then the correct output is -1.

Your result should be a single non-negative integer for a valid position or -1 if no position satisfies the requirement. Do not modify the list, and do not reorder any values. Focus on the relationship between the accumulating sum and the target, paying attention to the earliest point at which the requirement is fulfilled. Return the leftmost index where the total meets or surpasses the target, ensuring consistency when multiple adjacent positions would also exceed it.

Example 1:

Input: nums = [2, 3, 1, 5], target = 5
Output: 1
Explanation: Running totals are [2, 5, 6, 11]; the first time we reach at least 5 is at index 1.

Example 2:

Input: nums = [1, 1, 1, 1], target = 3
Output: 2
Explanation: Running totals are [1, 2, 3, 4]; index 2 is the first position with total ≥ 3.

Example 3:

Input: nums = [4, 4], target = 10
Output: -1
Explanation: Running totals are [4, 8]; the target is never reached.

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.

BudiBadu Logo

Minimum Prefix Index Reaching Target

Binary Search Easy 1 views

You are given a list of positive integers ordered from left to right and a target value. As you move through the list, imagine maintaining a running total that grows by adding each number one by one. Your task is to determine the smallest index at which this running total becomes greater than or equal to the target. If you reach the end of the list and the running total never reaches the target, then there is no valid position and the correct answer is -1.

Think of each element as a step that raises a cumulative counter. At the beginning, the counter is zero. With every step, the counter increases. The moment the counter meets or exceeds the target, the position where this happens is the answer you should return. Because all values are positive, the running total increases steadily, and once it has crossed the target, it will never dip below it. If the target is less than or equal to the first element, then the answer is index 0, since the running total reaches the requirement immediately. If the target is larger than the sum of all elements, then the correct output is -1.

Your result should be a single non-negative integer for a valid position or -1 if no position satisfies the requirement. Do not modify the list, and do not reorder any values. Focus on the relationship between the accumulating sum and the target, paying attention to the earliest point at which the requirement is fulfilled. Return the leftmost index where the total meets or surpasses the target, ensuring consistency when multiple adjacent positions would also exceed it.

Example 1:

Input: nums = [2, 3, 1, 5], target = 5
Output: 1
Explanation: Running totals are [2, 5, 6, 11]; the first time we reach at least 5 is at index 1.

Example 2:

Input: nums = [1, 1, 1, 1], target = 3
Output: 2
Explanation: Running totals are [1, 2, 3, 4]; index 2 is the first position with total ≥ 3.

Example 3:

Input: nums = [4, 4], target = 10
Output: -1
Explanation: Running totals are [4, 8]; the target is never reached.

00:00
Loading editor...
Test Results

Run your code to see test results

Click the Submit button to execute your solution

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.