2763. Sum of Imbalance Numbers of All Subarrays Hard
1/**
2 * [2763] Sum of Imbalance Numbers of All Subarrays
3 *
4 * The imbalance number of a 0-indexed integer array arr of length n is defined as the number of indices in sarr = sorted(arr) such that:
5 *
6 * 0 <= i < n - 1, and
7 * sarr[i+1] - sarr[i] > 1
8 *
9 * Here, sorted(arr) is the function that returns the sorted version of arr.
10 * Given a 0-indexed integer array nums, return the sum of imbalance numbers of all its subarrays.
11 * A subarray is a contiguous non-empty sequence of elements within an array.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [2,3,1,4]
16 * Output: 3
17 * Explanation: There are 3 subarrays with non-zero imbalance numbers:
18 * - Subarray [3, 1] with an imbalance number of 1.
19 * - Subarray [3, 1, 4] with an imbalance number of 1.
20 * - Subarray [1, 4] with an imbalance number of 1.
21 * The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3.
22 *
23 * <strong class="example">Example 2:
24 *
25 * Input: nums = [1,3,3,3,5]
26 * Output: 8
27 * Explanation: There are 7 subarrays with non-zero imbalance numbers:
28 * - Subarray [1, 3] with an imbalance number of 1.
29 * - Subarray [1, 3, 3] with an imbalance number of 1.
30 * - Subarray [1, 3, 3, 3] with an imbalance number of 1.
31 * - Subarray [1, 3, 3, 3, 5] with an imbalance number of 2.
32 * - Subarray [3, 3, 3, 5] with an imbalance number of 1.
33 * - Subarray [3, 3, 5] with an imbalance number of 1.
34 * - Subarray [3, 5] with an imbalance number of 1.
35 * The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8.
36 *
37 * Constraints:
38 *
39 * 1 <= nums.length <= 1000
40 * 1 <= nums[i] <= nums.length
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/
46// discuss: https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn sum_imbalance_numbers(nums: Vec<i32>) -> i32 {
52 0
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_2763() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.