Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].Return the sum of the squares of distinct counts of all subarrays of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1] Output: 15 Explanation: Six possible subarrays are: [1]: 1 distinct value [2]: 1 distinct value [1]: 1 distinct value [1,2]: 2 distinct values [2,1]: 2 distinct values [1,2,1]: 2 distinct values The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [1,1] Output: 3 Explanation: Three possible subarrays are: [1]: 1 distinct value [1]: 1 distinct value [1,1]: 1 distinct value The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100Problem summary: You are given a 0-indexed integer array nums. The distinct count of a subarray of nums is defined as: Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j]. Return the sum of the squares of distinct counts of all subarrays of nums. A subarray is a contiguous non-empty sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,1]
[1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2913: Subarrays Distinct Element Sum of Squares I
class Solution {
public int sumCounts(List<Integer> nums) {
int ans = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
int[] s = new int[101];
int cnt = 0;
for (int j = i; j < n; ++j) {
if (++s[nums.get(j)] == 1) {
++cnt;
}
ans += cnt * cnt;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2913: Subarrays Distinct Element Sum of Squares I
func sumCounts(nums []int) (ans int) {
for i := range nums {
s := [101]int{}
cnt := 0
for _, x := range nums[i:] {
s[x]++
if s[x] == 1 {
cnt++
}
ans += cnt * cnt
}
}
return
}
# Accepted solution for LeetCode #2913: Subarrays Distinct Element Sum of Squares I
class Solution:
def sumCounts(self, nums: List[int]) -> int:
ans, n = 0, len(nums)
for i in range(n):
s = set()
for j in range(i, n):
s.add(nums[j])
ans += len(s) * len(s)
return ans
// Accepted solution for LeetCode #2913: Subarrays Distinct Element Sum of Squares I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2913: Subarrays Distinct Element Sum of Squares I
// class Solution {
// public int sumCounts(List<Integer> nums) {
// int ans = 0;
// int n = nums.size();
// for (int i = 0; i < n; ++i) {
// int[] s = new int[101];
// int cnt = 0;
// for (int j = i; j < n; ++j) {
// if (++s[nums.get(j)] == 1) {
// ++cnt;
// }
// ans += cnt * cnt;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2913: Subarrays Distinct Element Sum of Squares I
function sumCounts(nums: number[]): number {
let ans = 0;
const n = nums.length;
for (let i = 0; i < n; ++i) {
const s: number[] = Array(101).fill(0);
let cnt = 0;
for (const x of nums.slice(i)) {
if (++s[x] === 1) {
++cnt;
}
ans += cnt * cnt;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.