
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find if array can be divided into two subarrays of equal sum in C++
Suppose we have an array A. We have to check whether we can split the array into two parts, whose sum are equal. Suppose the elements are [6, 1, 3, 2, 5], then [6, 1], and [2, 5] can be two subarrays.
This problem can be solved easily by following these rules. We have to find the sum of all elements of the array at first, then for each element of the array, we can calculate the right sum by using total_sum – sum of elements found so far.
Example
#include<iostream> #include<numeric> using namespace std; void displaySubArray(int arr[], int left, int right) { cout << "[ "; for (int i = left; i <= right; i++) cout << arr[i] << " "; cout << "] "; } void subarrayOfSameSum(int arr[] , int n) { int total_sum = accumulate(arr, arr+n, 0); int so_far_sum = 0; for(int i = 0; i<n; i++){ if(2*so_far_sum+arr[i] == total_sum){ cout << "subarray 1: "; displaySubArray(arr, 0, i-1); cout << "\nsubarray 2: "; displaySubArray(arr, i+1, n-1); return; } so_far_sum += arr[i]; } cout << "No subarray can be formed"; } int main() { int arr[] = {6, 1, 3, 2, 5} ; int n = sizeof(arr)/sizeof(arr[0]); subarrayOfSameSum(arr, n); }
Output
subarray 1: [ 6 1 ] subarray 2: [ 2 5 ]
- Related Articles
- Find the sums for which an array can be divided into subarrays of equal sum in Python
- Check if a large number can be divided into two or more segments of equal sum in C++
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python
- Can array be divided into n partitions with equal sums in JavaScript
- Check if any square (with one colored cell) can be divided into two equal parts in Python
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Convert array into array of subarrays - JavaScript
- Find an element which divides the array in two subarrays with equal product in Python
- Finding n subarrays with equal sum in JavaScript
- Can parasites be divided into partial and complete parasites?
- Check if it possible to partition in k subarrays with equal sum in Python
- Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)
- Maximum Sum of Two Non-Overlapping Subarrays in C++

Advertisements