
- 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 Sum of all unique subarray sum for a given array in C++
In this problem, we are given an array arr[] consisting of n integer values. Our task is to find the sum of all unique subarray sum for a given array. Subarray sum is the sum of elements of the given subarray.
Let's take an example to understand the problem,
Input : arr[] = {1, 2, 4} Output : 23
Explanation −
All subarrays of the given array are : (1), (2), (4), (1, 2), (2, 4), (1, 2, 4) Sum of subarrays = 1 + 2 + 4 + (1+2) + (2+4) + (1+2+4) = 23
Solution Approach
A solution to the problem is by storing the subarray sum and then sorting them to find unique once. Then we will consider all unique subarrays for the sum.
Algorithm
Step 1 − Find the sum of all sub-arrays and store it in a vector.
Step 2 − Sort the vector.
Step 3 − Consider all the vectors which are unique and mark the sum of rest to 0.
Step 4 − Calculate and print the sum.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; long int findSumOfSubArraySum(int arr[], int n){ int i, j; long int sumArrayTill[n + 1] = { 0 }; for (i = 0; i < n; i++) sumArrayTill[i + 1] = sumArrayTill[i] + arr[i]; vector<long int> subArraySum; for (i = 1; i <= n; i++) for (j = i; j <= n; j++) subArraySum.push_back(sumArrayTill[j] - sumArrayTill[i - 1]); sort(subArraySum.begin(), subArraySum.end()); for (i = 0; i < subArraySum.size() - 1; i++){ if (subArraySum[i] == subArraySum[i + 1]) { j = i + 1; while (subArraySum[j] == subArraySum[i] && j < subArraySum.size()){ subArraySum[j] = 0; j++; } subArraySum[i] = 0; } } long sum = 0; for (i = 0; i < subArraySum.size(); i++) sum += subArraySum[i]; return sum; } int main(){ int arr[] = { 1, 2, 4, 7, 9 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The sum of all unique subarray sum is "<<findSumOfSubArraySum(arr, n); return 0; }
Output
The sum of all unique subarray sum is 144
Another approach using Iteration
Another approach to solve the problem is using a hash table. We will find the subarray sums and store them in a hash table and increment hash count. Then find the sum of all unique subarrays (subarray with hash count 1).
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; long int findSumOfSubArraySum(int arr[], int n){ int sumSubArraySum = 0; unordered_map<int, int> sumSubArray; for (int i = 0; i < n; i++) { int sum = 0; for (int j = i; j < n; j++) { sum += arr[j]; sumSubArray[sum]++; } } for (auto itr : sumSubArray) if (itr.second == 1) sumSubArraySum += itr.first; return sumSubArraySum; } int main(){ int arr[] = { 1, 2, 4, 7, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The sum of all unique subarray sum is "<<findSumOfSubArraySum(arr, n); return 0; }
Output
The sum of all unique subarray sum is 124
- Related Articles
- Program to find sum of the 2 power sum of all subarray sums of a given array in Python
- Find subarray with given sum - (Nonnegative Numbers) in C++
- All unique triplets that sum up to a given value in C++
- How to find the sum of all elements of a given array in JavaScript?
- Find subarray with given sum - (Handles Negative Numbers) in C++
- Maximum Subarray Sum in a given Range in C++
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- Finding sum of all unique elements in JavaScript
- Check for Subarray in the original array with 0 sum JavaScript
- Program to find kpr sum for all queries for a given list of numbers in Python
- Maximum subarray sum in array formed by repeating the given array k times in C++
- Find the sum of maximum difference possible from all subset of a given array in Python
- Maximum subarray sum in circular array using JavaScript
- Maximize the subarray sum after multiplying all elements of any subarray with X in C++
- Finding the sum of unique array values - JavaScript
