2
public int maxSumAfterPartitioning(int[] arr, int k) {3
return maxSum(arr, k, 0);6
public int maxSum(int[] arr, int k, int start) {7
int curr1 = 0, curr2 = 0;11
Map<String, Integer> memo = new HashMap();12
// memo.put("0,0", arr[0]);14
for (int i = 0; i < arr.length; ++i) {15
// without current element16
curr1 = prev + arr[i];18
// with current element, find max if p=0...k (since subarray can be longeth of at most k)19
int tempk = 0, half1 = 0, half2 = 0, temp = 0;20
for (int p = 0; p <= k; ++p) {21
half1 = findMaxSumWithKEle(arr, p, i);23
half2 = memo.get(("0," + tempk)) == null ? 0 : memo.get(("0," + tempk));24
if (temp < half1 + half2) {31
// find max between curr1 or curr2 - with current elemtn in the subarray or outside the33
max = (curr1 < curr2) ? curr2 : curr1;36
String key = "0," + i;38
System.out.println("Max: " + max + " from [" + key + "]");45
public static Integer findMaxSumWithKEle(int[] arr, int k, int end) {47
if (end > arr.length || end < 0) {51
for (int i = end; i > (end - k) && i >= 0; --i) {