1995. Count Special Quadruplets LeetCode Solution

In this guide, you will get 1995. Count Special Quadruplets LeetCode Solution with the best time and space complexity. The solution to Count Special Quadruplets problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Special Quadruplets solution in C++
  4. Count Special Quadruplets solution in Java
  5. Count Special Quadruplets solution in Python
  6. Additional Resources
1995. Count Special Quadruplets LeetCode Solution image

Problem Statement of Count Special Quadruplets

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

Example 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 <= 50
1 <= nums[i] <= 100

Complexity Analysis

  • Time Complexity: O(n^4)
  • Space Complexity: O(1)

1995. Count Special Quadruplets LeetCode Solution in C++

class Solution {
 public:
  int countQuadruplets(std::vector<int>& nums) {
    const int n = nums.size();
    int ans = 0;

    for (int a = 0; a < n; ++a)
      for (int b = a + 1; b < n; ++b)
        for (int c = b + 1; c < n; ++c)
          for (int d = c + 1; d < n; ++d)
            if (nums[a] + nums[b] + nums[c] == nums[d])
              ++ans;

    return ans;
  }
};
/* code provided by PROGIEZ */

1995. Count Special Quadruplets LeetCode Solution in Java

class Solution {
  public int countQuadruplets(int[] nums) {
    final int n = nums.length;
    int ans = 0;

    for (int a = 0; a < n; ++a)
      for (int b = a + 1; b < n; ++b)
        for (int c = b + 1; c < n; ++c)
          for (int d = c + 1; d < n; ++d)
            if (nums[a] + nums[b] + nums[c] == nums[d])
              ++ans;

    return ans;
  }
}
// code provided by PROGIEZ

1995. Count Special Quadruplets LeetCode Solution in Python

class Solution:
  def countQuadruplets(self, nums: list[int]) -> int:
    n = len(nums)
    return sum(nums[a] + nums[b] + nums[c] == nums[d]
               for a in range(n)
               for b in range(a + 1, n)
               for c in range(b + 1, n)
               for d in range(c + 1, n))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.