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; }
Pingback: Interview Question & Answers – Migratory Birds Count – 11 – CODERZHEAVEN