We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Divisible Sum Pairs
Divisible Sum Pairs
Sort by
recency
|
2401 Discussions
|
Please Login in order to post a comment
The problem specification indicates that all index pairs (i, j) where i < j and ar[i] + ar[j] is divisible by k must be found. Therefore, all possible pairs of elements in the array should be considered, as long as they meet the condition that i is less than j.
If we review the array [1, 3, 2, 6, 1, 2], the pairs are formed by checking each possible combination of indices (i, j) where i < j. In this case, the pair (4, 5) is also valid because:
ar[4] = 1 and ar[5] = 2 1 + 2 = 3, and 3 is divisible by 3. So the pair (4, 5) also meets the condition.
Here is the complete list of pairs that meet the divisibility condition by 3:
(0, 2) → 1 + 2 = 3 (0, 5) → 1 + 2 = 3 (2, 3) → 2 + 6 = 8 (2, 4) → 2 + 1 = 3 (3, 4) → 6 + 1 = 7 (4, 5) → 1 + 2 = 3
In summary, the pair (4, 5) should also be considered, as it meets the conditions, so the result should be 6 and not 5. Is there an error in the problem explanation and the result should be 6, or is the result indeed 5 and the condition that the pair (4, 5) was excluded not explained? What is the exact criterion for evaluating the pairs to be considered?
Haskell:
The easy, brute force way (solve) and the better, single-pass way (solve2).
PHP
Python
Time complexity: O(n)
int sum = 0; int count= 0;