Problem: So basically, I'm supposed to code a recursive program that takes in a set of integers that represent the file sizes of different files, and I'm to use those numbers to maximize the amount of space used on a floppy disk (a disk has a max size of 1440kb). My program is supposed to return the minimum amount of space achievable with the specific set of files. Also, files can be placed on a disk multiple times. I've tried to do the question and this is what I've come up with:

  public static int findMinSpace (int[] set, int spaceUsed) {
    int compare = 0;
    int cur;
    if (spaceUsed == MAXSPACE) {
      return 0;
    } else if (spaceUsed > MAXSPACE) {
      return -1;
    } else {
      for (int i = 0; i < set.length; i++) {
        cur = findMinSpace(set, spaceUsed + set[i]);
        if (cur == -1) {
          cur = MAXSPACE - spaceUsed;
        }
        if (cur < compare || compare == 0) {
          compare = cur;
        }
      }
      return compare;
    }
  }

When the method is initially called, I pass in the array of integers (file sizes), and the number 0 (since no files are placed yet). Also MAXSPACE is a global variable. So basically it seems that for some combinations of numbers the program works, while for others it doesn't (at least in comparison of outputs compared to the problem's outputs). Also when I use one of the several examples in the problem the program just seems to run for a very long time (but doesn't overflow), but seeing as how this is an example from the problem, I doubt it should take this long to compute making me believe I have some steps that are incorrect. I've tried to trace through the code over and over again for the last answer and I just cannot figure out what I'm doing wrong.

Any help would be greatly appreciated. Also if there's anything in the code that I should clarify, or if you wish to see any other code please do ask.