package leetcode;

/**
 * https://leetcode.com/problems/count-special-quadruplets/
 */
public class Problem1995 {
    public int countQuadruplets(int[] nums) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1995 prob = new Problem1995();
        System.out.println(prob.countQuadruplets(new int[]{1,2,3,6})); // 1
        System.out.println(prob.countQuadruplets(new int[]{3,3,6,4,5})); // 0
        System.out.println(prob.countQuadruplets(new int[]{1,1,1,3,5})); // 4
    }
}

package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/count-special-quadruplets/
 */
public class Problem1995 {
    public int countQuadruplets(int[] nums) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(nums[i])) {
                map.put(nums[i], new ArrayList<>());
            }
            map.get(nums[i]).add(i);
        }
        int answer = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    int sum = nums[i] + nums[j] + nums[k];
                    for (int index : map.getOrDefault(sum, List.of())) {
                        if (k < index) {
                            answer++;
                        }
                    }
                }
            }
        }
        return answer;
    }
}

Solve problem 1995