id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

frew@mclean.com recursion-2point5 > maxSum
prev  |  next  |  chance

Write a recursive method called maxSum that accepts a list of integers, L, and an integer limit n as parameters and uses backtracking to find the maximum sum that can be generated by adding elements of L that do not exceed n. For example, if you are given the list [7,30,8,22,6,1,14] and the limit of 19, the maximum sum that can be generated that does not exceed is 16, achieved by adding 7, 8, and 1. If the list is empty, or if the limit is not a positive integer, or all of L’s values exceed the limit, return 0. Each index’s element in the list can be added to the sum only once, but the same number value might occur more than once in a list, in which case each occurrence might be added to the sum. For example, if the list is [6,2,1] you may use up to one 6 in the sum, but if the list is [6,2,6,1] you may use up to two sixes. You may assume that all values in the list are nonnegative. Your method may alter the contents of the list as it executes, but should be restored to its original state before your method returns. Do not use any loops.


maxSum(19, [7, 30, 8, 22, 6, 1, 14], 0) → 16
maxSum(42, [5, 30, 15, 13, 8], 0) → 41
maxSum(40, [30, 15, 20], 0) → 35

...Save, Compile, Run (ctrl-enter)

public int maxSum(int limit, int[] nums, int index) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 400

Copyright Nick Parlante 2017 - privacy