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

 

test2023_06_01_BONUS_allWays


This is a BONUS question for everyone! However, you MUST have a RECURSIVE solution in order to earn the bonus. This is a TOUGH problem involving branching, back-track recursion. (I am NOT joking! That is a hint.) Write a method that returns the number of different ways you can get 'targetSum' by adding any number of numbers from the 'nums' array. As a precondition, you may assume that 'nums' is non-empty and that no two numbers in 'nums' match each other (they are all distinct numbers). Please note that it might not be possible to get 'targetSum' by adding any combination of numbers in 'nums', in which case you would return a zero. Also note that it may be possible to get 'targetSum' just by "adding" one number from 'nums', in which case you would return a 1. Preconditions: 'nums' has at least one element. No two elements in 'nums' are the same. I suggest using a helper method that is basically just the whole method, but with a much shorter name. (That way your recursive call will not be so annoyingly large.) You might also want to add some additional parameters (or maybe at least ONE additional parameter. I only used one additional parameter).


test2023_06_01_BONUS_allWays([4, 18, 22, 45, 70, 60, 30, 19, 23, 72, 14, 63, 700, 42, 405], 91) → 5
test2023_06_01_BONUS_allWays([5, 15, 7, 13, 8, 2, 18, 12, 17, 3, 1, 6, 19, 14, 11, 9], 20) → 27
test2023_06_01_BONUS_allWays([48, 53, 71, 91, 100], 60) → 0

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

public int test2023_06_01_BONUS_allWays(int[] nums, int targetSum) { }

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: 430

Copyright Nick Parlante 2017 - privacy