Given an array of integers arr of even length n and an integer k.
We want to divide the array into exactly n / 2 pairs such that the sum of each pair is divisible by k.
Return true If you can find a way to do that or false otherwise.
Example 1:
Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).
Example 2:
Input: arr = [1,2,3,4,5,6], k = 7 Output: true Explanation: Pairs are (1,6),(2,5) and(3,4).
Example 3:
Input: arr = [1,2,3,4,5,6], k = 10 Output: false Explanation: You can try all possible pairs to see that there is no way to divide arr into 3 pairs each with sum divisible by 10.
Constraints:
arr.length == n1 <= n <= 105n is even.-109 <= arr[i] <= 1091 <= k <= 105The problem can be approached by first sorting the data of interest and then using the two-pointer technique to solve it efficiently. This method leverages the sorted nature of the data to reduce the complexity of finding comparisons or sums. The two-pointer technique usually involves placing two pointers at different positions in the array and moving them according to the condition checks on the sum or comparison of their pointed values. This allows for efficient traversal with lesser computations.
This C code first sorts the array using quicksort for simplicity and then uses two pointers to find if there are any two numbers in the array which sum up to 0. The sorted array helps in deciding which pointer to move based on the sum of the pointed numbers. This reduces the problem to linear complexity after sorting.
C++
Time Complexity: O(n log n) due to sorting and O(n) due to the two-pointer technique, resulting in O(n log n).
Space Complexity: O(1) since the solution uses no additional space except input array.
An efficient alternative to sorting could be the usage of a hash table (or unordered map) that enables O(1) average-time complexity for search operations. By inserting each element into the hash table and checking if the complementary value (to make a sum zero) exists, one can get the desired pair in linear time without needing to sort the entire array.
This Python solution uses a dictionary to store elements. It then checks for each element if its negation exists in the dictionary. If a complement is found, a solution is printed, otherwise the iteration continues until all elements are processed.
Java
Time Complexity: O(n) on average for insertion and look-up.
Space Complexity: O(n) due to storage in the hash map.
| Approach | Complexity |
|---|---|
| Approach 1: Use Sorting and Two-Pointer Technique | Time Complexity: O(n log n) due to sorting and O(n) due to the two-pointer technique, resulting in O(n log n). |
| Approach 2: Utilize Hashing for Faster Look-up | Time Complexity: O(n) on average for insertion and look-up. |
Subarray Sums Divisible by K - Leetcode 974 - Python • NeetCodeIO • 23,675 views views
Watch 9 more video solutions →Practice Check If Array Pairs Are Divisible by k with our built-in code editor and test cases.
Practice on FleetCode