BackeasyArrays

Count Elements Exceeding Prefix Sum Solution

Problem Statement

Given an array of integers `nums`, find the number of indices `i` (where `0 <= i < nums.length`) such that the sum of all elements before index `i` is strictly less than the value of `nums[i]`. Note that the sum of elements before index `0` is defined to be `0`.

Example 1
Input
nums = [3, 4, 1, 10]
Output
3

Explanation: We evaluate each index: - i = 0: Sum of elements before index 0 is 0. Since 3 > 0, this index is counted. Cumulative sum becomes 3. - i = 1: Sum of elements before index 1 is 3. Since 4 > 3, this index is counted. Cumulative sum becomes 7. - i = 2: Sum of elements before index 2 is 7. Since 1 is not > 7, this index is not counted. Cumulative sum becomes 8. - i = 3: Sum of elements before index 3 is 8. Since 10 > 8, this index is counted. Cumulative sum becomes 18. Total count is 3.

Example 2
Input
nums = [-1, -2, 5]
Output
1

Explanation: We evaluate each index: - i = 0: Sum of elements before index 0 is 0. Since -1 is not > 0, this index is not counted. Cumulative sum becomes -1. - i = 1: Sum of elements before index 1 is -1. Since -2 is not > -1, this index is not counted. Cumulative sum becomes -3. - i = 2: Sum of elements before index 2 is -3. Since 5 > -3, this index is counted. Cumulative sum becomes 2. Total count is 1.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Live Compiler
Sign InSign Up
Loading...
Test Cases & Output
Click "Run" to execute