Help me in solving COPRIME3 problem

My issue

.

My code


import java.util.*;

class Codechef {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[] a = new int[N];

        for (int i = 0; i < N; i++) {
            a[i] = scanner.nextInt();
        }

        Map<Integer, Integer> frequency = new HashMap<>();
        for (int num : a) {
            frequency.put(num, frequency.getOrDefault(num, 0) + 1);
        }

        List<Integer> uniqueNumbers = new ArrayList<>(frequency.keySet());
        long totalCount = (long) N * (N - 1) * (N - 2) / 6;
        long totalTriplets = 0;

        for (int gcd = 1; gcd <= 100000; gcd++) {
            long count = 0;

            for (int num : uniqueNumbers) {
                if (num % gcd == 0) {
                    count += frequency.get(num);
                }
            }

            if (count >= 3) {
                totalTriplets += (count * (count - 1) * (count - 2)) / 6;
            }
        }

        long validTriplets = totalCount - totalTriplets;

        System.out.println(validTriplets);
    }
}

Problem Link: Coprime Triples Practice Coding Problem