
- 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
Maximum average sum partition of an array in C++
Problem statement
Given an array, we partition a row of numbers A into at most K adjacent (non-empty) groups, then the score is the sum of the average of each group. What is the maximum score that can be scored?
Example
If input array is {9, 2, 5, 3, 10} then we can partition array as follows −
{9} {2, 5, 3} and {10} then average sum of this is −
9 + (2 + 5 + 3)/ 3 + 10 = 22.33
Algorithm
We can use memorization technique to solve this problem −
- Let memo[i][k] be the best score portioning A[i to n-1] into at most K partss
- In the first group, we partition A[i to n-1] into A[i to j-1] and A[j to n-1], then our candidate partition has score average(i, j) + score(j, k-1)), where average(i, j) = (A[i] + A[i+1] + … + A[j-1]) / (j – i). We take the highest score of these
- In total, our recursion in the general case is : memo[n][k] = max(memo[n][k], score(memo, i, A, k-1) + average(i, j))
Example
Let us now see an example −
#include <bits/stdc++.h> using namespace std; define MAX 1000 double memo[MAX][MAX]; double score(int n, vector<int>& arr, int k) { if (memo[n][k] > 0) { return memo[n][k]; } double sum = 0; for (int i = n - 1; i > 0; i--) { sum += arr[i]; memo[n][k] = max(memo[n][k], score(i, arr, k - 1) + sum / (n - i)); } return memo[n][k]; } double getLargestSum(vector<int>& arr, int K) { int n = arr.size(); double sum = 0; memset(memo, 0.0, sizeof(memo)); for (int i = 0; i < n; i++) { sum += arr[i]; memo[i + 1][1] = sum / (i + 1); } return score(n, arr, K); } int main() { vector<int> arr = {9, 2, 5, 3, 10}; int K = 3; cout << "Largest sum = " << getLargestSum(arr, K) << endl; return 0; }
Output
When you compile and execute above program. It generates following output -
Largest sum = 22.3333
- Related Articles
- Partition Array for Maximum Sum in Python
- Maximum equlibrium sum in an array in C++
- Equal partition of an array of numbers - JavaScript
- Partition Array Into Three Parts With Equal Sum in Python
- Maximum sum of smallest and second smallest in an array in C++
- Calculating average of an array in JavaScript
- Maximum sum of pairwise product in an array with negative allowed in C++
- Maximum sum of smallest and second smallest in an array in C++ Program
- Partition Equal Subset Sum in C++
- Find average of each array within an array in JavaScript
- Maximum sum of pairwise product in an array with negative allowed in C++ program
- Array Partition I in Python
- Maximum subarray sum in an array created after repeated concatenation in C++
- Maximum triplet sum in array in C++
- Find average of each array within an array JavaScript

Advertisements