Problem 52

Can Make Playlist Recursion Challenge

Given an array of song lengths, a target workout length, and a maximum number of times each song can be used, determine whether a playlist can be built that adds up exactly to the workout length.

Function Signature

canMakePlaylist(songLengths, workoutLength, maxTimes)

Parameters

  • songLengths — an array of non-negative integers representing available song durations.
  • workoutLength — a non-negative integer representing the exact target duration.
  • maxTimes — a non-negative integer representing the maximum number of times each song can be used.

Output

Return true if a valid combination of songs can sum to exactly workoutLength, or false otherwise.

Constraints

  • The function must use recursion.
  • Each song may be used at most maxTimes times.
  • The input array must not be mutated.
  • The function should always accept exactly three arguments.

Examples

canMakePlaylist([3, 5, 7], 10, 2)  → true   (3 + 7)
canMakePlaylist([4], 8, 2)         → true   (4 + 4)
canMakePlaylist([4], 12, 2)        → false  (would need 4 three times)
canMakePlaylist([5, 9], 8, 3)      → false
canMakePlaylist([3, 5], 0, 2)      → true   (empty playlist)
canMakePlaylist([], 5, 2)          → false
canMakePlaylist([2, 3], 7, 2)      → true   (2 + 2 + 3)
canMakePlaylist([2, 3], 9, 2)      → false

Edge Cases

  • A workout length of 0 is always achievable (empty playlist).
  • An empty song list with a non-zero workout length returns false.
  • If maxTimes is 0, no songs can be used, so only workout length 0 returns true.