Talentd
FleetCode

4051. Count Subarrays with Distant Sums

Hard32.8% acceptance

You are given an integer array nums and two integers goal and k.

A subarray nums[i..j] is considered distant if the absolute difference between its sum and goal is at least k.

Return the number of distant subarrays.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [1,2,1], goal = 4, k = 1

Output: 5

Explanation:

The distant subarrays for k = 1 are:

i j nums[i..j] Sum abs(sum - goal)
0 0 [1] 1 3
1 1 [2] 2 2
2 2 [1] 1 3
0 1 [1, 2] 3 1
1 2 [2, 1] 3 1

Thus, the answer is 5.

Example 2:

Input: nums = [2,-1,3], goal = 2, k = 2

Output: 2

Explanation:

The distant subarrays for k = 2 are:

i j nums[i..j] Sum abs(sum - goal)
1 1 [-1] -1 3
0 2 [2, -1, 3] 4 2

Thus, the answer is 2.

Example 3:

Input: nums = [-3,1,2], goal = 0, k = 3

Output: 2

Explanation:

The distant subarrays for k = 3 are:

i j nums[i..j] Sum abs(sum - goal)
0 0 [-3] -3 3
1 2 [1, 2] 3 3

Thus, the answer is 2.

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • -109 <= goal <= 109
  • 0 <= k <= 109

Hints open one at a time

Hint 1

1a (Fenwick Tree). If k = 0, every subarray is distant. Otherwise, for a current prefix sum s and an earlier prefix sum p, the subarray is distant when p <= s - goal - k or p >= s - goal + k.

Hint 2

1b (Fenwick Tree). Compress the prefix sums and process them from left to right. Use a Fenwick tree to count earlier prefix sums in these two ranges, querying before inserting the current prefix sum.

Hint 3

2a (Merge Sort). Count the complementary subarrays whose sums satisfy goal - k < sum < goal + k, then subtract this count from n * (n + 1) / 2. Handle k = 0 directly.

Hint 4

2b (Merge Sort). Apply merge sort to the prefix sums. At each merge, use two pointers across the sorted halves to count differences strictly between the two bounds.

42:42
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def distantSubarrays(self, nums: list[int], goal: int, k: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,1]
4
1
Input
[2,-1,3]
2
2
Input
[-3,1,2]
0
3
Your code is saved on this device.Solution