import java.util.Arrays;
class SplitCoin {
public int split(int[] coins) {
int[] upper = null;
int[] lower = null;
int smallestDifference = 0;
lower
= Arrays.
copyOfRange(coins,
0,coins.
length-1); upper
= Arrays.
copyOfRange(coins, coins.
length-1,coins.
length); //(after sorting) put the largest element in upper
smallestDifference
= Math.
abs(arraySum
(upper
) - arraySum
(lower
)); return findSmallestDifference(lower, upper, arraySum(lower), arraySum(upper), smallestDifference);
}
private int findSmallestDifference (int[] lower, int[] upper, int lowerSum, int upperSum, int smallestDifference) {
int[] newUpper = null, newLower = null;
int currentDifference
= Math.
abs(upperSum
-lowerSum
); if (currentDifference < smallestDifference) {
smallestDifference = currentDifference;
}
if (lowerSum < upperSum || lower.length < upper.length || lower[0] > currentDifference
|| lower[lower.length-1] > currentDifference
|| lower[lower.length-1] < upper[0]/lower.length) {
return smallestDifference;
}
for (int i = lower.length-1; i >= 0 && smallestDifference > 0; i--) {
newUpper = addElement(upper, lower[i]);
newLower = removeElementAt(lower, i);
smallestDifference = findSmallestDifference(newLower, newUpper,
lowerSum - lower[i], upperSum + lower [i], smallestDifference);
}
return smallestDifference;
}
private int arraySum (int[] array) {
int result = 0;
for (int current : array){
result += current;
}
return result;
}
private int[] addElement (int[] array, int element) {
int[] result = new int[array.length+1];
System.
arraycopy(array,
0, result,
0, array.
length); result[array.length] = element;
return result;
}
private int[] removeElementAt (int[] array, int index) {
int[] result = new int[array.length-1];
System.
arraycopy(array,
0, result,
0, index
); System.
arraycopy(array, index
+1, result, index, result.
length-index
); return result;
}
public static void main
(String[] args
) { //debug int[] temp = {851,886,91,128,942,395,976,872,337,334};
/*
int[] temp = {100000,919,445,108,329,566,51,611,69,289,
479,671,916,185,667,450,988,107,861,668,186,277,
123,551,352,350,997,189,654,151};
*/
/*
int[] temp = {100000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000};
*/
SplitCoin sc = new SplitCoin();
System.
out.
println(sc.
split(temp
)); }
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgpjbGFzcyBTcGxpdENvaW4gewogICAgcHVibGljIGludCBzcGxpdChpbnRbXSBjb2lucykgewogICAgICAgIGludFtdIHVwcGVyID0gbnVsbDsKICAgICAgICBpbnRbXSBsb3dlciA9IG51bGw7CiAgICAgICAgaW50IHNtYWxsZXN0RGlmZmVyZW5jZSA9IDA7CiAgICAgICAgCiAgICAgICAgQXJyYXlzLnNvcnQoY29pbnMpOwoKICAgICAgICBsb3dlciA9IEFycmF5cy5jb3B5T2ZSYW5nZShjb2lucywgMCxjb2lucy5sZW5ndGgtMSk7CiAgICAgICAgdXBwZXIgPSBBcnJheXMuY29weU9mUmFuZ2UoY29pbnMsIGNvaW5zLmxlbmd0aC0xLGNvaW5zLmxlbmd0aCk7IC8vKGFmdGVyIHNvcnRpbmcpIHB1dCB0aGUgbGFyZ2VzdCBlbGVtZW50IGluIHVwcGVyICAgICAgICAgICAgCgogICAgICAgIHNtYWxsZXN0RGlmZmVyZW5jZSA9IE1hdGguYWJzKGFycmF5U3VtKHVwcGVyKSAtIGFycmF5U3VtKGxvd2VyKSk7CiAgICAgICAgcmV0dXJuIGZpbmRTbWFsbGVzdERpZmZlcmVuY2UobG93ZXIsIHVwcGVyLCBhcnJheVN1bShsb3dlciksIGFycmF5U3VtKHVwcGVyKSwgc21hbGxlc3REaWZmZXJlbmNlKTsKICAgIH0KICAgIAogICAgcHJpdmF0ZSBpbnQgZmluZFNtYWxsZXN0RGlmZmVyZW5jZSAoaW50W10gbG93ZXIsIGludFtdIHVwcGVyLCBpbnQgbG93ZXJTdW0sIGludCB1cHBlclN1bSwgaW50IHNtYWxsZXN0RGlmZmVyZW5jZSkgewogICAgICAgIGludFtdIG5ld1VwcGVyID0gbnVsbCwgbmV3TG93ZXIgPSBudWxsOwogICAgICAgIGludCBjdXJyZW50RGlmZmVyZW5jZSA9IE1hdGguYWJzKHVwcGVyU3VtLWxvd2VyU3VtKTsKICAgICAgICBpZiAoY3VycmVudERpZmZlcmVuY2UgPCBzbWFsbGVzdERpZmZlcmVuY2UpIHsKICAgICAgICAgICAgc21hbGxlc3REaWZmZXJlbmNlID0gY3VycmVudERpZmZlcmVuY2U7CiAgICAgICAgfSAKICAgICAgICBpZiAobG93ZXJTdW0gPCB1cHBlclN1bSB8fCBsb3dlci5sZW5ndGggPCB1cHBlci5sZW5ndGggfHwgbG93ZXJbMF0gPiBjdXJyZW50RGlmZmVyZW5jZSAKICAgICAgICAgICAgICAgIHx8IGxvd2VyW2xvd2VyLmxlbmd0aC0xXSA+IGN1cnJlbnREaWZmZXJlbmNlIAogICAgICAgICAgICAgICAgfHwgbG93ZXJbbG93ZXIubGVuZ3RoLTFdIDwgdXBwZXJbMF0vbG93ZXIubGVuZ3RoKSB7CiAgICAgICAgICAgIHJldHVybiBzbWFsbGVzdERpZmZlcmVuY2U7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSBsb3dlci5sZW5ndGgtMTsgaSA+PSAwICYmIHNtYWxsZXN0RGlmZmVyZW5jZSA+IDA7IGktLSkgeyAgICAgICAgICAgCiAgICAgICAgICAgbmV3VXBwZXIgPSBhZGRFbGVtZW50KHVwcGVyLCBsb3dlcltpXSk7CiAgICAgICAgICAgbmV3TG93ZXIgPSByZW1vdmVFbGVtZW50QXQobG93ZXIsIGkpOwogICAgICAgICAgIHNtYWxsZXN0RGlmZmVyZW5jZSA9IGZpbmRTbWFsbGVzdERpZmZlcmVuY2UobmV3TG93ZXIsIG5ld1VwcGVyLCAKICAgICAgICAgICAgICAgICAgIGxvd2VyU3VtIC0gbG93ZXJbaV0sIHVwcGVyU3VtICsgbG93ZXIgW2ldLCBzbWFsbGVzdERpZmZlcmVuY2UpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gc21hbGxlc3REaWZmZXJlbmNlOwogICAgfQogICAgCiAgICBwcml2YXRlIGludCBhcnJheVN1bSAoaW50W10gYXJyYXkpIHsKICAgICAgICBpbnQgcmVzdWx0ID0gMDsgICAgICAgIAogICAgICAgIGZvciAoaW50IGN1cnJlbnQgOiBhcnJheSl7CiAgICAgICAgICAgIHJlc3VsdCArPSBjdXJyZW50OwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgfQogICAgCiAgICBwcml2YXRlIGludFtdIGFkZEVsZW1lbnQgKGludFtdIGFycmF5LCBpbnQgZWxlbWVudCkgewogICAgICAgIGludFtdIHJlc3VsdCA9IG5ldyBpbnRbYXJyYXkubGVuZ3RoKzFdOwogICAgICAgIFN5c3RlbS5hcnJheWNvcHkoYXJyYXksIDAsIHJlc3VsdCwgMCwgYXJyYXkubGVuZ3RoKTsKICAgICAgICByZXN1bHRbYXJyYXkubGVuZ3RoXSA9IGVsZW1lbnQ7CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgIH0KICAgIAogICAgcHJpdmF0ZSBpbnRbXSByZW1vdmVFbGVtZW50QXQgKGludFtdIGFycmF5LCBpbnQgaW5kZXgpIHsKICAgICAgICBpbnRbXSByZXN1bHQgPSBuZXcgaW50W2FycmF5Lmxlbmd0aC0xXTsKICAgICAgICBTeXN0ZW0uYXJyYXljb3B5KGFycmF5LCAwLCByZXN1bHQsIDAsIGluZGV4KTsKICAgICAgICBTeXN0ZW0uYXJyYXljb3B5KGFycmF5LCBpbmRleCsxLCByZXN1bHQsIGluZGV4LCByZXN1bHQubGVuZ3RoLWluZGV4KTsKICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgfQogICAgCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7IC8vZGVidWcKICAgICAgICBpbnRbXSB0ZW1wID0gezg1MSw4ODYsOTEsMTI4LDk0MiwzOTUsOTc2LDg3MiwzMzcsMzM0fTsKLyogICAgICAKICAgICAgICBpbnRbXSB0ZW1wID0gezEwMDAwMCw5MTksNDQ1LDEwOCwzMjksNTY2LDUxLDYxMSw2OSwyODksCiA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