# Count Of Subarrays With Sum Can Be Divided By K

Count Of Subarrays With Sum Can Be Divided By K

In the content of programming, the array data structures are studied in nearly all the programming languages. But, the core concepts in all the programming languages still remain the same such as the Subarrays.

A Subarray is commonly referred to as the contiguous part of an array i.e an array within an array itself.

This is because the Subarray is used for storing the type of data required in a program that can be used for further iterative purposes.

You can also navigate through the data structure of a Subarray just as you would with an array using the pointers. The elements stored within a Subarray also have specific addresses which makes it extremely easier to navigate through them.

So now the question arises, how many Subarrays can be stored within an Array of size N?

Well, you can essentially store N * (N+1)/2 Subarrays in a program that is basically the half of each of the elements stored in an array.

Based on this information, we intend to discuss an important Subarray problem that is commonly asked in the technical interviews.

Let us figure out the number of Subarray sums can be divided by k that can be spotted in an array.

What are Subarrays?

The subarrays are commonly referred to as the contiguous parts of an array that are located in different parts of the array.

You can say that a Subarray is basically an array inside another array. They mostly occupy consecutive positions in an array and are used for maintaining the sequence of elements within an array.

This means that a Subarray is basically used for storing elements from the array that are arranged in the same sort of sequence as depicted in the original array.

Hence, the nature of a subarray and an array are essentially the same.

For instance, let us consider the example of an array to understand how a Subarray can be formed using the same array:

arr = [1, 2, 3, 4]

subarray = [1, 2] [1, 3] [2, 3]

From the above example, it is clear that 1 is the actual size of the array.

By now you might have gained a proper understanding of the concept of Subarrays. In this blog, we are looking at a problem statement where we will be counting all the subarray sum divisible by k.

In order to properly comprehend this problem, let’s dive into the problem statement and deal with all the plausible methods that can be applied for finding a solution to this problem.

What are the methods to count all Subarrays that are can be divided by k?

In this problem, we are given the task of counting all the subarray sum divisible by k. This means that we will have to figure out all the Subarrays that have a sum that is can be divided by k.

Problem Statement

You have been given an array. This array consists of positive integers and/or negative integers along with a value k. Your challenge is finding the number Of all the subarrays whose sum might be divided by k.

For the simple solution to this problem, we can start by calculating the sum of all the given Subarrays in the problem and check whether any of them can be divided by k.

Here are the algorithms for this approach:

• Start by creating an auxiliary array with the size k that will be defined by Mod[k]. This array will be used for holding the count for each of the remainder that we will be getting after dividing the cumulative sum of the array till any of the index
• Now we can start calculating the cumulative sum and simultaneously consider the mod along with k. Whatever reminder that we get can increment the count by 1. This will be used as a reminder for an  index in the Mod[] auxiliary array
• The Subarray for positions of each of the pairs within the same value for cumSum % k will make up for a continuous range that will have a sum that would be can be divided by k
• Now, you need to traverse the Mod[] auxiliary array. For every   Mod[i] that is greater than 1 divisible by K, you can select any two given pairs of indices i.e (Mod[i]*(Mod[i]-1))/2 number of ways.
• You can perform the same actions for all of the remainders that is greater than k and find the summation of the results that will represent the number of all the possible Subarrays that are can be divided by k

This approach can also be used for solving the Subarray with 0 sum problem. The implementation of this approach in C++ would look something as follows:

• Use the namespace std; function for handling all of the cases
• This function will be used for counting all Subarray sum can be divided by k
• You can modify this function to handle all the negative numbers
• You can form an auxiliary hash to calculate the frequency of the remainders
• Next up, you need to find the summation in order to increase the count with 1 in the remainder
• Since there might be a few negative sum, you can consider the value of mod twice
• Now, traverse the mod and add all the elements that can be divided by k. Note that the sum of these elements will be 0

The possible time complexity after applying this approach will be O(n+k)

Final Thoughts

Accuracy and feasibility are two important aspects that are facilitated by the subarrays. They are essentially used for storing additional information that was not initially stored within the array.

In the context of programming, we have quite a few Subarray based problems that are common for coding interviews such as Subarray with sum 0 problems or merging two given sorted arrays.