# Number of times an array can be partitioned repetitively into subarrays with equal sum

In C++ we have a vector header file that can change the size of an array during runtime. In this article, we are going to learn the concept of the number of times an array can be partitioned repetitively into subarrays with equal sum.

Let’s take an example to show an array partition with an equal sum.

The given array is {1,2,3,4,2} and we are subdividing the array into two parts −

{1,2,3}- The total sum of each index of an array is 6.

{4,2}- The total sum of each index of an array is 6.

So, 2 times the number of the given arrays can be partitioned into subarrays with equal sum.

## Algorithm

• We will start the program with header files namely ‘iostream’ and ‘vector’.

• Now we start the program by creating a class named ‘isPartition_arr’.

In the public section, declare the constructor named ‘isPartition_arr’ which accepts num as a parameter for addressing the array element values.

• We are defining a function named ‘cnt_Partition’ of integer type to calculate the total number of times an array can be partitioned.

• We are initializing the variable ‘sum’ to ‘0’ which will later meet the array for addition and storing ‘0’ to the variable ‘count’ which will keep track of an incrementing count of array elements. Then declaring the for loop to iterate over each element of the ‘arr’ vector.

• We are initializing the variable ‘current_sum’ to ‘0’ and iterating over each element using for loop.

• After completing for loop, we start using the while loop to iterate over each element.

If the ‘current_sum’ is equivalent to ‘sum/2’ then the count will be incremented by ‘1’ and reset ‘current_sum’ to ‘0’. Next, returning ‘cnt’ will count the number of times an array can be subdivided into equal sums.

• We start with the main function to create a ‘num’ integer of vector type to store the array values.

• Then we pass the ‘num’ value by creating the object of the class. Next, we call the function ‘cnt_partition’ by taking the object and storing it in the variable ‘c’.

• Finally, we print the output statement as “Number of times of an array can be partitioned into two subarrays with equal sum” with the help of variable ‘c’.

## Example

In this program, we are going to find the number of times an array can be partitioned repetitively into two subarrays with equal sum.

#include <iostream>
#include <vector>
using namespace std;
class isPartition_arr {
public:
vector<int> arr;
isPartition_arr(vector<int>& num) {
arr = num;
}
int cnt_Partition() {
int sum = 0, count = 0;
for (int i = 0; i < arr.size(); i++) {
sum += arr[i];
}
int current_sum = 0, j=0;
while( j < arr.size() ) {
current_sum += arr[j];
if (current_sum == sum / 2) {
current_sum = 0;
count++;
}
j++;
}
return count;
}
};
int main() {
vector<int> num = {1, 2, 3, 4, 5, 5};
isPartition_arr A(num);
int c = A.cnt_Partition();
cout <<"Number of times an array can be partitioned into\t"<< c <<"\t two subarrays with equal sum " << endl;
return 0;
}


## Output

Number of times an array can be partitioned into   2   two subarrays with equal sum


## Conclusion

We explored the concept of array partition with equal sum where we learn how arrays are sub-dividing in different parts and make the total sum equivalent. We are using the Oops concept to solve this problem because of the better readability of code and defining a C++ program efficiently.

Updated on: 10-May-2023

58 Views 