Home  >  Article  >  Backend Development  >  The number of times an array can be repeatedly split into subarrays with equal sums

The number of times an array can be repeatedly split into subarrays with equal sums

PHPz
PHPzforward
2023-09-06 12:21:071036browse

The number of times an array can be repeatedly split into subarrays with equal sums

In C, we have a vector header file that can change the size of the array at runtime. In this article, we will learn the concept of how many times an array can be repeatedly split into subarrays with equal sums.

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

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

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

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

So, 2 times the size of a given array can be split into subarrays with equal sums.

Algorithm

  • We will start the program with the header files 'iostream' and 'vector'.

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

    In the public section, declare a constructor named 'isPartition_arr', which accepts num as a parameter to resolve the value of the array element.

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

  • We initialize the variable 'sum' to '0', which will be used later to sum and store the array '0' To the variable 'count', used to track the incrementing count of array elements. Then declare a 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 the for loop, we start using a while loop to iterate over each element.

    If 'current_sum' is equal to 'sum/2', then the count will be incremented by '1' and 'current_sum' Reset to '0'. Next, return 'cnt' will count the number of times the array can be divided equally.

  • We start from the main function and create a 'num' vector of type integer to store the value of the array.

  • 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'.

The Chinese translation of

Example

is:

Example

In this program, we will find the number of times an array can be repeatedly split into two subarrays so that their sums are equal.

#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 

in conclusion

We explored the concept of equal sum array division and learned how to split an array into different parts and make the sums equal. We use object-oriented concepts to solve this problem because the code is more readable and a C program can be defined efficiently.

The above is the detailed content of The number of times an array can be repeatedly split into subarrays with equal sums. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete