Given an array of N non-negative integers, task is to find the maximum size of a subarray such that the pairwise sum of the elements of this subarray is not divisible by a given integer, K. Also, print this subarray as well. If there are two or more subarrays which follow the above stated condition, then print the first one from the left.
Prerequisite : Subset with no pair sum divisible by K
Input : arr = [3, 7, 1, 9, 2] K = 3 Output : 3 [3, 7, 1] 3 + 7 = 10, 3 + 1 = 4, 7 + 1 = 8, all are not divisible by 3. It is not possible to get a subarray of size bigger than 3 with the above-mentioned property. [7, 1, 9] is also of the same size but [3, 7, 1] comes first. Input : arr = [2, 4, 4, 3] K = 4 Output : 2 [2, 4] 2 + 4 = 6 is not divisible by 4. It is not possible to get a subarray of size bigger than 2 with the above-mentioned property. [4, 3] is also of the same size but [2, 4] comes first.
Naive Approach :
The naive method would be to consider all the subarrays. While considering a subarray, take elements pairwise and compute the sum of the two elements of the pair. If the computed sum is divisible by K, then ignore this subarray and continue with the next subarray. Else, compute the sum of other pairs of this subarray in a similar fashion. If no pair’s sum is a multiple of K, then compare the size of this subarray with the maximum size obtained so far and update if required.
The time complexity of this method would be O().
Efficient Approach(Using Hashing) :
We create an empty hash table and insert arr % k into it. Now we traverse remaining elements and maintain a window such that no pair in the window is divisible by k. For every traversed element, we remove starting elements while there exist an element in current window which makes a divisible pair with current element. To check if there is an element in current window, we check if following.
1) If there is an element x such that (K – x % K) is equal to arr[i] % K
2) OR arr[i] % k is 0 and it exists in the hash.
Once we make sure that all elements which can make a pair with arr[i] are removed, we add arr[i] to current window and check if size of current window is more than the maximum window so far.
The maximum size is 2 and the subarray is as follows 10 15
Time Complexity : O(n)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Subset with no pair sum divisible by K
- Longest subarray with sum divisible by k
- Longest subarray with elements divisible by k
- Find a subarray whose sum is divisible by size of the array
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Pair with given product | Set 1 (Find if any pair exists)
- Maximum length of subarray such that all elements are equal in the subarray
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that sum of the subarray is even
- Count of subarray that does not contain any subarray with sum 0
- Find a pair with given sum in BST
- Pair with given sum in matrix
- Linked List Pair Sum
- Subarray of size k with given sum
- Subarray whose sum is closest to K
- Find if there is a subarray with 0 sum
- Longest subarray having sum K | Set 2
- XOR of all subarray XORs | Set 1
- Given an array A and a number x, check for pair in A with sum as x
- Pair with given sum and maximum shortest distance from end
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.