Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
886 B
21
Indexable
Never
public class Solution {
    public int solve(int[] A, int B) {
        int mod = 1000000007;
        HashMap<Integer, Long> map = new HashMap<>();
        for(int i = 0 ; i < A.length; i++){
            map.put(A[i] % B, map.getOrDefault(A[i] % B, 0l) + 1);
        }
        
        //case of 0
        long ans = 0;
        if(map.containsKey(0) == true){
            long f = map.get(0);
            ans = (ans + (f * (f - 1)) / 2) % mod;
        }
        if(B % 2 == 0 && map.containsKey(B/2) == true){
            long f = map.get(B / 2);
            ans = (ans + (f * (f - 1)) / 2) % mod;
        }
        
        for(int i = 1; i < (B+1) / 2; i++){
            long a = map.containsKey(i) == true ? map.get(i) : 0l;
            long b = map.containsKey(B - i) == true ? map.get(B - i) : 0l;
            ans = (ans + (a * b)) % mod;
        }
        return (int)ans;
    }
}