Interview Question & Answer – 10 – Divisible Sum pairs

By | October 21, 2018

Question

Find the count where the number k is divisible by sum of two numbers in the array and first number index should be less than second number index.

Function has the following parameter(s):

n: the integer length of array
ar: an array of integers
k: the integer to divide the pair sum by

You can download the problem statement here.

Solution

 static int divisibleSumPairs(int n, int k, int[] ar) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i < j && (ar[i] + ar[j]) % k == 0) {
                ++count;
            }
        }
    }
    return count;
}

One thought on “Interview Question & Answer – 10 – Divisible Sum pairs

  1. Pingback: Interview Question & Answers – Migratory Birds Count – 11 – CODERZHEAVEN

Leave a Reply

Your email address will not be published. Required fields are marked *