Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Best Partition You are given an array of positive numbers of size N and an integer K . You need to partition the array into
Best Partition
You are given an array of positive numbers of size and an integer K You need to partition the array into K
continuous segments. For each segment, the sum of its elements needs to be calculated.
The segment with the minimum sum is called the bestSegment and the sum of the elements of the
bestSegment is called the bestSum.
For all possible combinations of partitions of the array when divided into K segments, their bestSum needs to be
calculated and the one among them with maximum value needs to be returned.
My stuff Input Specification:
input: an array of positive numbers
input: an integer denoting the length of the array
input: an integer
Output Specification:
Return an integer denoting the maximum value of all possible bestSum.
Example :
input :
input:
input:
Output :
Explanation:
You can partition the given array into continuous segments in the following mannerinput:
Output :
Explanation:
You can partition the given array into continuous segments in the following manner
the sum of individual segments is and the bestSum is
the sum of individual segments is and the bestSum is
the sum of individual segments is and the bestSum is
The maximum value of bestSum among is So will be returned as the answer.
Example :
input :
input:
input:
Output :
Explanation:
You can partition the given array into continuous segments only in one way
The sum of individual segments is and the bestSum is
Since the only value of bestSum is it will be returned as the answer. Previously I have contacted expert in Chegg. He provided this code public class MaxBestSum
public static int maxBestSumint arr, int N int K
if arr null N K K N
return ; Invalid input
int dp new intK N ;
for int k ; k K; k
for int n k; n N; n
int currentSum ;
int bestSum Integer.MAXVALUE;
for int i n; i k; i
currentSum arri ;
bestSum Math.minbestSum Math.maxdpk i currentSum;
dpkn bestSum;
return dpKN;
public static void mainString args
int inputArray ;
int N ;
int K ;
int result maxBestSuminputArray N K;
System.out.printlnMaximum bestSum: result;
But this code doesn't works for nd example please rectify the error in this code and provide me in Java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started