search
HomeBackend DevelopmentC++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

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. If there is any infringement, please contact admin@php.cn delete
MobileSAM:为移动设备提供高性能的轻量级图像分割模型MobileSAM:为移动设备提供高性能的轻量级图像分割模型Jan 05, 2024 pm 02:50 PM

一、引言随着移动设备的普及和计算能力的提升,图像分割技术成为了研究的热点。MobileSAM(MobileSegmentAnythingModel)是一种针对移动设备优化的图像分割模型,旨在在保持高质量分割结果的同时,降低计算复杂度和内存占用,以便在资源有限的移动设备上高效运行。本文将详细介绍MobileSAM的原理、优势和应用场景。二、MobileSAM模型的设计思路MobileSAM模型的设计思路主要包括以下几个方面:轻量级模型:为了适应移动设备的资源限制,MobileSAM模型采用了轻量级

如何在Python中使用图像语义分割技术?如何在Python中使用图像语义分割技术?Jun 06, 2023 am 08:03 AM

随着人工智能技术的不断发展,图像语义分割技术已经成为图像分析领域的热门研究方向。在图像语义分割中,我们将一张图像中的不同区域进行分割,并对每个区域进行分类,从而达到对这张图像的全面理解。Python是一种著名的编程语言,其强大的数据分析和数据可视化能力使其成为了人工智能技术研究领域的首选。本文将介绍如何在Python中使用图像语义分割技术。一、前置知识在深入

Python 教程:如何使用 Python 分割和合并大文件?Python 教程:如何使用 Python 分割和合并大文件?Apr 22, 2023 am 11:43 AM

有时候,我们需要把一个大文件发送给别人,但是限于传输通道的限制,比如邮箱附件大小的限制,或者网络状况不太好,需要将大文件分割成小文件,分多次发送,接收端再对这些小文件进行合并。今天就来分享一下用Python分割合并大文件的方法。思路及实现如果是文本文件,可以按行数分割。无论是文本文件还是二进制文件,都可以按指定大小进行分割。使用Python的文件读写功能就可以实现文件的分割与合并,设置每个文件的大小,然后读取指定大小的字节就写入一个新文件,接收端依次读取小文件,把读取到的字节按序写入一个文件,就

php 怎么求2个数组相同的元素php 怎么求2个数组相同的元素Dec 23, 2022 am 10:04 AM

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

c语言数组如何初始化c语言数组如何初始化Jan 04, 2023 pm 03:36 PM

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

Golang与FFmpeg: 如何实现音频合成和分割Golang与FFmpeg: 如何实现音频合成和分割Sep 27, 2023 pm 10:52 PM

Golang与FFmpeg:如何实现音频合成和分割,需要具体代码示例摘要:本文将介绍如何使用Golang和FFmpeg库来实现音频合成和分割。我们将用到一些具体的代码示例来帮助读者更好地理解。引言:随着音频处理技术的不断发展,音频合成和分割已经成为日常生活和工作中常见的功能需求。而Golang作为一种快速,高效且易于编写和维护的编程语言,加上FFmpeg作

减小win10录屏文件大小的建议减小win10录屏文件大小的建议Jan 04, 2024 pm 12:05 PM

许多的小伙伴都需要录屏进行办公或者传输文件,但是有时候会出现文件过大的问题制造了很多麻烦,下面就给大家带来了文件过大的解决方法,一起看看吧。win10录屏文件太大怎么办:1、下载软件格式工厂来进行压缩文件。下载地址>>2、进入主页面,点击“视频-MP4”选项。3、在转换格式页面中点击“添加文件”,选择要压缩的MP4文件。4、点击页面“输出配置”,通过输出质量来压缩文件。5、下拉配置列表选择“低质量和大小”点击“确定”。6、点击“确定”完成视频文件的导入。7、点击“开始”进行转化。8、完成后即可

c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor