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.
Given a 0-indexed integer array nums, return the number of distinct quadruplets (a, b, c, d) such that:
nums[a] + nums[b] + nums[c] == nums[d], anda < b < c < dExample 1:
Input: nums = [1,2,3,6] Output: 1 Explanation: The only quadruplet that satisfies the requirement is (0, 1, 2, 3) because 1 + 2 + 3 == 6.
Example 2:
Input: nums = [3,3,6,4,5] Output: 0 Explanation: There are no such quadruplets in [3,3,6,4,5].
Example 3:
Input: nums = [1,1,1,3,5] Output: 4 Explanation: The 4 quadruplets that satisfy the requirement are: - (0, 1, 2, 3): 1 + 1 + 1 == 3 - (0, 1, 3, 4): 1 + 1 + 3 == 5 - (0, 2, 3, 4): 1 + 1 + 3 == 5 - (1, 2, 3, 4): 1 + 1 + 3 == 5
Constraints:
4 <= nums.length <= 501 <= nums[i] <= 100Problem summary: Given a 0-indexed integer array nums, return the number of distinct quadruplets (a, b, c, d) such that: nums[a] + nums[b] + nums[c] == nums[d], and a < b < c < d
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,6]
[3,3,6,4,5]
[1,1,1,3,5]
4sum)increasing-triplet-subsequence)count-good-triplets)count-increasing-quadruplets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1995: Count Special Quadruplets
class Solution {
public int countQuadruplets(int[] nums) {
int ans = 0, n = nums.length;
for (int a = 0; a < n - 3; ++a) {
for (int b = a + 1; b < n - 2; ++b) {
for (int c = b + 1; c < n - 1; ++c) {
for (int d = c + 1; d < n; ++d) {
if (nums[a] + nums[b] + nums[c] == nums[d]) {
++ans;
}
}
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1995: Count Special Quadruplets
func countQuadruplets(nums []int) int {
ans, n := 0, len(nums)
for a := 0; a < n-3; a++ {
for b := a + 1; b < n-2; b++ {
for c := b + 1; c < n-1; c++ {
for d := c + 1; d < n; d++ {
if nums[a]+nums[b]+nums[c] == nums[d] {
ans++
}
}
}
}
}
return ans
}
# Accepted solution for LeetCode #1995: Count Special Quadruplets
class Solution:
def countQuadruplets(self, nums: List[int]) -> int:
ans, n = 0, len(nums)
for a in range(n - 3):
for b in range(a + 1, n - 2):
for c in range(b + 1, n - 1):
for d in range(c + 1, n):
if nums[a] + nums[b] + nums[c] == nums[d]:
ans += 1
return ans
// Accepted solution for LeetCode #1995: Count Special Quadruplets
struct Solution;
impl Solution {
fn count_quadruplets(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut res = 0;
for i in 0..n {
for j in i + 1..n {
for k in j + 1..n {
for l in k + 1..n {
if nums[i] + nums[j] + nums[k] == nums[l] {
res += 1;
}
}
}
}
}
res
}
}
#[test]
fn test() {
let nums = vec![1, 2, 3, 6];
let res = 1;
assert_eq!(Solution::count_quadruplets(nums), res);
let nums = vec![3, 3, 6, 4, 5];
let res = 0;
assert_eq!(Solution::count_quadruplets(nums), res);
let nums = vec![1, 1, 1, 3, 5];
let res = 4;
assert_eq!(Solution::count_quadruplets(nums), res);
}
// Accepted solution for LeetCode #1995: Count Special Quadruplets
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1995: Count Special Quadruplets
// class Solution {
// public int countQuadruplets(int[] nums) {
// int ans = 0, n = nums.length;
// for (int a = 0; a < n - 3; ++a) {
// for (int b = a + 1; b < n - 2; ++b) {
// for (int c = b + 1; c < n - 1; ++c) {
// for (int d = c + 1; d < n; ++d) {
// if (nums[a] + nums[b] + nums[c] == nums[d]) {
// ++ans;
// }
// }
// }
// }
// }
// 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.