💥💥 Beats 100% on runtime and memory [EXPLAINED]
IntuitionFind imbalance numbers between all possible subarrays of the given list. An imbalance number is calculated based on the number of times a number differ
# Intuition
Find imbalance numbers between all possible subarrays of the given list. An imbalance number is calculated based on the number of times a number differs from others in a subarray. The challenge is to compute this efficiently without checking every single subarray directly.

# Approach
Track the "last seen index" of each number using an array and calculate the "right edge" for each element while iterating backward through the list, then compute the imbalance for each number while iterating forward, leveraging these previously calculated edges.

# Complexity
- Time complexity:
O(N) because we go through the list a constant number of times, performing constant-time operations for each element.

- Space complexity:
O(N) because we use arrays to track the last seen index and right edge for each element.

# Code
```csharp []
public class Solution {
    public int SumImbalanceNumbers(int[] nums) {
        int N = nums.Length;

        // Initialize an array to store the last seen index for each number
        int[] lastIndex = new int[N + 2];
        Array.Fill(lastIndex, N);

        // Array to store the right edge for each element
        int[] rightEdge = new int[N];
        for (int i = N - 1; i >= 0; --i) {
            int num = nums[i];
            rightEdge[i] = Math.Min(lastIndex[num], lastIndex[num + 1]);
            lastIndex[num] = i;
        }

        int count = 0;

        // Reset lastIndex to track left edge positions
        Array.Fill(lastIndex, -1);
        for (int i = 0; i < N; ++i) {
            int num = nums[i];
            int leftEdge = lastIndex[num + 1];
            count += (i - leftEdge) * (rightEdge[i] - i);
            lastIndex[num] = i;
        }

        // Subtract the total number of subarrays from the result
        return count - N * (N + 1) / 2;
    }
}

```
['Array', 'Hash Table', 'Math', 'Binary Search', 'Dynamic Programming', 'Binary Search Tree', 'Ordered Map', 'Binary Tree', 'Ordered Set', 'C#']