• + 0 comments

    Java:

    public static int pairs(int k, List<Integer> arr) {
        // Create a HashSet for quick lookups
        HashSet<Integer> set = new HashSet<>(arr);
        int numOfpairs = 0;
    
        // Loop through each element in the set
        for (int num : set) {
            // Check if the pair (num + k) exists in the set
            if (set.contains(num - k)) {
                numOfpairs++;
            }
        }
        
        return numOfpairs;
    }