Divisible Sum Pairs

You are given an array of  integers, , and a positive integer, . Find and print the number of  pairs where  and  +  is divisible by .

Input Format

The first line contains  space-separated integers,  and .
The second line contains  space-separated integers describing the values of .

Constraints

Output Format

Print the number of  pairs where  and  +  is evenly divisible by .

Sample Input

6 3
1 3 2 6 1 2
Sample Output

 5
Explanation

Here are the  valid pairs when :


public class Solution {

    static int divisibleSumPairs(int n, int k, int[] ar) {
        int result=0;
        for(int i=0;i<ar.length;i++){
            for(int j=i+1;j<ar.length;j++){
                int sum=ar[i]+ar[j];
                //System.out.println("ar[i]+ar[j] "+ar[i]+"  "+ar[j]+" sum " +sum+" "+ "k- "+k);
                
                if(sum%k==0){
                   // System.out.println("sum%k "+sum%k);
                    result++;
                }
            }
        }return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int result = divisibleSumPairs(n, k, ar);
        System.out.println(result);
    }
}

Post a Comment

Previous Post Next Post