Problem solution · Java

Sum of Imbalance Numbers of All Subarrays

Sum of Imbalance Numbers of All Subarrays: a Java solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
43 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Sum of Imbalance Numbers of All Subarrays, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 43 lines of Java from the credited upstream file 2763-2.java.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 3 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeSum of Imbalance Numbers of All Subarrays · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  // If sorted(nums)[i + 1] - sorted(nums)[i] > 1, then there's a gap. Instead  // of determining the number of gaps in each subarray, let's find out how many  // subarrays contain each gap.  public int sumImbalanceNumbers(int[] nums) {    final int n = nums.length;    int ans = 0;    // Note that to avoid double counting, only `left` needs to check nums[i].    // This adjustment ensures that i represents the position of the leftmost    // element of nums[i] within the subarray.     // left[i] := the maximum index l s.t. nums[l] = nums[i] or nums[i] + 1    int[] left = new int[n];    // right[i] := the minimum index r s.t. nums[r] = nums[i]    int[] right = new int[n];    int[] numToIndex = new int[n + 2];     Arrays.fill(numToIndex, -1);    for (int i = 0; i < n; ++i) {      left[i] = Math.max(numToIndex[nums[i]], numToIndex[nums[i] + 1]);      numToIndex[nums[i]] = i;    }     Arrays.fill(numToIndex, n);    for (int i = n - 1; i >= 0; --i) {      right[i] = numToIndex[nums[i] + 1];      numToIndex[nums[i]] = i;    }     // The gap above nums[i] persists until encountering nums[i] or nums[i] + 1.    // Consider subarrays nums[l..r] with l <= i <= r, where l in [left[i], i]    // and r in [i, right[i] - 1]. There are (i - left[i]) * (right[i] - i)    // subarrays satisfying this condition.    for (int i = 0; i < n; ++i)      ans += (i - left[i]) * (right[i] - i);     // Subtract n * (n + 1) / 2 to account for the overcounting of elements    // initially assumed to have a gap. This adjustment is necessary as the    // maximum element of every subarray does not have a gap.    return ans - n * (n + 1) / 2;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗