Given an array of size N, we need to find smallest sized subarray whose sum is divisible by array size N.
Input : arr = [1, 1, 2, 2, 4, 2] Output : [2 4] Size of array, N = 6 Following subarrays have sum as multiple of N [1, 1, 2, 2], [2, 4], [1, 1, 2, 2, 4, 2] The smallest among all is [2 4]
We can solve this problem considering below facts,
Let S[i] denotes sum of first i elements i.e. S[i] = a + a .. + a[i] Now subarray arr(i, i + x) has sum multiple of N then, (arr(i] + arr[i+1] + .... + arr[i + x])) % N = 0 (S[i+x] – S[i] ) % N = 0 S[i] % N = S[i + x] % N
We need to find the minimum value of x for which the above condition holds. This can be implemented in single iteration with O(N) time-complexity using another array modIdx of size N. Array modIdx is initialized with all elements as -1. modIdx[k] is to be updated with i in each iteration, where k = sum % N.
Now in each iteration we need to update modIdx[k] according to the value of sum % N.
We need to check two things,
If at any instant k = 0 and it is the first time we are updating modIdx (i.e. modIdx was -1)
Then we assign x to i + 1, because (i + 1) will be the length of subarray whose sum is multiple of N
In other case whenever we get a mod value, if this index is not -1, that means it is updated by some other sum value, whose index is stored at that index, we update x with this difference value, i.e. by i – modIdx[k].
After each above operation we update the minimum value of length and corresponding starting index and end index for the subarray. Finally, this gives the solution to our problem.
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Smallest subarray whose product leaves remainder K when divided by size of the array
- First subarray having sum at least half the maximum sum of any subarray of size K
- Find a subarray whose sum is divisible by size of the array
- Find a subarray of size K whose sum is a perfect square
- Smallest subarray of size greater than K with sum greater than a given value
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Check if a subarray of size K exists whose elements form a number divisible by 3
- Create an array of size N with sum S such that no subarray exists with sum S or S-K
- Smallest subarray from a given Array with sum greater than or equal to K
- Smallest Subarray with Sum K from an Array
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Find array elements equal to sum of any subarray of at least size 2
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Count of integers in an Array whose length is a multiple of K
- Count of elements in an Array whose set bits are in a multiple of K
- Print the lexicographically smallest array by swapping elements whose sum is odd
- Subsequences of size three in an array whose sum is divisible by m
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Construct an Array of size N whose sum of cube of all elements is a perfect square