Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1995.html

1995. Count Special Quadruplets

Level

Easy

Description

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

Solution

Loop over nums and count the number of quadruplets that satisfies the two conditions.

  • class Solution {
        public int countQuadruplets(int[] nums) {
            int count = 0;
            int length = nums.length;
            for (int i = 0; i < length - 3; i++) {
                for (int j = i + 1; j < length - 2; j++) {
                    for (int k = j + 1; k < length - 1; k++) {
                        int sum = nums[i] + nums[j] + nums[k];
                        for (int m = k + 1; m < length; m++) {
                            if (sum == nums[m])
                                count++;
                        }
                    }
                }
            }
            return count;
        }
    }
    
    ############
    
    class Solution {
        public int countQuadruplets(int[] nums) {
            int ans = 0, n = nums.length;
            int[] counter = new int[310];
            for (int b = n - 3; b > 0; --b) {
                int c = b + 1;
                for (int d = c + 1; d < n; ++d) {
                    if (nums[d] - nums[c] >= 0) {
                        ++counter[nums[d] - nums[c]];
                    }
                }
                for (int a = 0; a < b; ++a) {
                    ans += counter[nums[a] + nums[b]];
                }
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/count-special-quadruplets/
    // Time: O(N^4)
    // Space: O(1)
    class Solution {
    public:
        int countQuadruplets(vector<int>& A) {
            int N = A.size(), ans = 0;
            for (int i = 0; i < N; ++i) {
                for (int j = i + 1; j < N; ++j) {
                    for (int k = j + 1; k < N; ++k) {
                        for (int t = k + 1; t < N; ++t) {
                            if (A[i] + A[j] + A[k] == A[t]) ++ans;
                        }
                    }
                }
            }
            return ans;
        }
    };
    
  • class Solution:
        def countQuadruplets(self, nums: List[int]) -> int:
            ans, n = 0, len(nums)
            counter = Counter()
            for b in range(n - 3, 0, -1):
                c = b + 1
                for d in range(c + 1, n):
                    counter[nums[d] - nums[c]] += 1
                for a in range(b):
                    ans += counter[nums[a] + nums[b]]
            return ans
    
    ############
    
    # 1995. Count Special Quadruplets
    # https://leetcode.com/problems/count-special-quadruplets/
    
    class Solution:
        def countQuadruplets(self, nums: List[int]) -> int:
            n = len(nums)
            res = 0
            
            for i in range(n):
                for j in range(i + 1, n):
                    for k in range(j + 1, n):
                        for z in range(k + 1, n):
                            if nums[i] + nums[j] + nums[k] == nums[z]:
                                res += 1
            
            return res
    
    
  • func countQuadruplets(nums []int) int {
    	ans, n := 0, len(nums)
    	counter := make([]int, 310)
    	for b := n - 3; b > 0; b-- {
    		c := b + 1
    		for d := c + 1; d < n; d++ {
    			if nums[d] >= nums[c] {
    				counter[nums[d]-nums[c]]++
    			}
    		}
    		for a := 0; a < b; a++ {
    			ans += counter[nums[a]+nums[b]]
    		}
    	}
    	return ans
    }
    

All Problems

All Solutions