0

I'm having some problems solving this question:

Given an array of ints, divide the input into 2 groups such that their sums are as close as possible, the 2 groups must be equal in length, or if the input is odd length then one group can have 1 more than the other. Then print the lower sum first, and the higher sum after.

Ex: input -> [4,6,17,3,2,5,10] output -> 23,24 ([17,5,2] , [10,6,4,3])

This is what I've come up with and so far what I've tested it's passed but I do not know if it's actually correct:

public static String closestSums(int[] input) {
    Integer sum1 = 0;
    Integer sum2 = 0;
    Integer dif = 0;
    Integer bigSum = 0;

    List<Integer> list = new ArrayList<>();
    List<Integer> list1 = new ArrayList<>();
    List<Integer> list2 = new ArrayList<>();

    for (int x = 0; x < input.length; x++) {
        list.add(input[x]);
    }

    Collections.sort(list);

    for (int x = list.size(); x >= 0; x--) {
        bigSum += list.get(x);
        if (dif == 0) {
            dif = list.get(x);
            list2.add(list.get(x));
        }
        else if (dif > 0) {
            dif -= list.get(x);
            list1.add(list.get(x));
        }
        else {
            dif += list.get(x);
            list2.add(list.get(x));
        }
    }

    dif = Math.abs(dif);
    if (dif != 0) {
        sum2 = (bigSum / 2) + dif;
        sum1 = bigSum / 2;
    }
    else {
        sum2 = bigSum / 2;
        sum1 = bigSum / 2;
    }
    return sum1 + ", " + sum2;
}
Abra
  • 19,142
  • 7
  • 29
  • 41
tHatpart
  • 1,302
  • 3
  • 12
  • 27
  • 1
    show us your effort so that we can help you out with it – Jyotirmay Dec 29 '20 at 04:44
  • posting my answer, but have to type it all in so it will take a minute or two – tHatpart Dec 29 '20 at 04:49
  • 1
    posted, hope that helps – tHatpart Dec 29 '20 at 04:55
  • _so far what I've tested it's passed but I do not know if it's actually correct_ If it passes all the tests, why wouldn't it be correct? Did you test different input arrays? Did you test an input array with only two elements? Did you test an input array with an even number of elements and with an odd number of elements? – Abra Dec 29 '20 at 05:31
  • See https://stackoverflow.com/a/65314921/585411 for a better approach. – btilly Dec 29 '20 at 17:03

2 Answers2

2

It's not correct.

Regardless of the bunch of small coding mistakes that are mentioned in the other answer, your algorithm doesn't work.

Consider the input [3, 3, 2, 2, 2]. Your algorithm will divide it into [3, 2, 2] and [3, 2] with a difference of 7-5=2. However, a better split with equal sums exists: [3, 3] and [2, 2, 2].

I am not going to provide a complete algorithm but give you a few hints:

1 - The minimum difference can be binary-searched. If you can come up with an algorithm that decides whether it is possible to split the array so that the difference of the sums of the pieces is at most d for a given d, then you can binary search the minimum d for which the algorithm outputs 1.

2 - Look at the subset sum problem, it can help with the subproblem I defined in item 1.

yemre
  • 708
  • 3
  • 11
1

I have made 3 observations after analyzing & running your Code.

  1. There lot of small errors throughout the code.

    • Syntaxical Error On Line 4 (Initialization of 'Dif') Line 11(The 'for' keyword)
    • Logical Error on Line 26, Line 27 (Instead of Accessing Index 'i', you must access index 'x')
    • Runtime Error on Line 17 (Initializing x=list.size() will throw a runtime error java.lang.IndexOutOfBoundsException)

    Though, the important things to look at, are mentioned below

  2. It's a suggestion, if you aren't printing the list with the 'sum1' & 'sum2', then creating and operating the two other output lists is redundant. The respective Sums of the list can be calculated while traversing the main list.

  3. Most importantly, the way you are calculating sum1 and sum2 after traversing the list and then dividing them, is not reliable. Eg. The Following input will give an unexpected output [4,6,45,3,2,5,10]. So, I suggest you to resort to the most reliable way of calculating the sum of list 1 & list 2, which is calculating them while traversing the list. (You may also remove the full logic of bigSum)

Last Words: Try and Implement the above mentioned Ideas yourself.

Sujit
  • 1,653
  • 2
  • 9
  • 25