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 &lt; b &lt; c &lt; d


&nbsp;
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


&nbsp;
Constraints:


	4 &lt;= nums.length &lt;= 50
	1 &lt;= nums[i] &lt;= 100


class Solution:
    def countQuadruplets(self, nums: List[int]) -> int:
        return sum([1 for a, b, c, d in combinations(nums, 4) if a + b + c == d])
class Solution {
    public int countQuadruplets(int[] nums) {
        int res = 0;
        int len = nums.length;
        
        Map<Integer, Integer> count = new HashMap<>();
        count.put(nums[len-1] - nums[len-2], 1);
        
        for (int b = len - 3; b >= 1; b--) {
            for (int a = b - 1; a >= 0; a--) {
                res += count.getOrDefault(nums[a] + nums[b], 0);
            }
            
            for (int x = len - 1; x > b; x--) {
                count.put(nums[x] - nums[b], count.getOrDefault(nums[x] - nums[b], 0) + 1);
            }
        }
        
        return res;
    }
}
class Solution {
public:
    int countQuadruplets(vector<int>& nums) {
        int n=nums.size();
        unordered_map<int,int> m;
        int ans=0;
        for(int i=1;i<n-2;i++){
            for(int j=0;j<i;j++){
                m[nums[i]+nums[j]]++;
            }
            for(int k=i+2;k<n;k++){
               ans+= m[nums[k]-nums[i+1]];
            }
        }
        return ans;
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var countQuadruplets = function(nums) {
    const map = new Map();
    
    for(let i = 0; i < nums.length; i++) {
        let n = nums[i];
        
        if(map.has(n)) {
            let t = map.get(n);
            t.push(i);
            map.set(n, t);
        } else {
            map.set(n, [i]);
        }
    }
    
    let count = 0;
    
    for(let a = 0;a < nums.length - 3; a++) {
        for(let b = a + 1; b < nums.length - 2; b++) {
            for(let c = b + 1; c < nums.length - 1;c++) {
                let sum = nums[a] + nums[b] + nums[c];
                
                let indexes = map.get(sum) || [];
				//t is the d index that might exist
                let t = c + 1;
                
                let low = 0;
                let high = indexes.length - 1;
                let ans = -1;
                
                while(low <= high) {
                    let mid = low + Math.floor((high - low)/2);
                    
                    if(indexes[mid] === t) {
                        ans = mid;
                        break;
                    }
                    
                    if(indexes[mid] < t) {
                        low = mid + 1;
                    } else {
                        ans = mid;
                        high = mid - 1;
                    }
                }
                
                if(ans !== -1) {
                    count += indexes.length - ans;
                }
            }
        }
    }
    
    return count;
};
Count Special Quadruplets