Array Pair Sum Divisibility
Given an array of integers and a number k, determine if there exists a pair of distinct elements in the array whose sum is divisible by k. The function should return true if such a pair exists and false otherwise.
[ [ 4, 5, 6, 7 ], 3 ]
Explanation. Here, 4 + 5 = 9 is divisible by 3. Hence, the output is true.
[ [ 33, 19, 28, 37 ], 10 ]
Explanation. There are no two numbers in this array whose sum is divisible by 10.
[ [ 22, 3, 6, -8, 4 ], 4 ]
Explanation. The pair (22, -8) sums to 14, which is divisible by 4. Therefore, the output is true.
[ [ 1 ], 5 ]
Explanation. There is only one element in the array, hence no pair exists.
[ [ 5, 25, 35, -5, 40 ], 10 ]
Explanation. Several pairs such as (25, 35), (35, -5), and others sum to multiples of 10. Thus, the output is true.
Follow-up: How would you optimize your solution if the array is very large or if it is known that all elements are non-negative?
- The array may contain both positive and negative integers.\n- The value of `k` will be a positive integer.\n- The array will contain at least one element.
- Views
- 3