Skip to main content
Back to problems
#1995
Easy Algorithms

Count special quadruplets

Array Hash Table Enumeration
64.4% acceptance
Feb 25, 2026
703
244
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

Solution

Rust
Time O(n³)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_quadruplets(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let mut count = 0;
    for a in 0..n {
      for b in a + 1..n {
        for c in b + 1..n {
          for d in c + 1..n {
            if nums[a] + nums[b] + nums[c] == nums[d] {
              count += 1;
            }
          }
        }
      }
    }
    count
  }
}