public static int[] existsInSortedSubset(int[] array, int contains) {
int testNo;
int[] indexTree = new int[array.length +2];
int currentLvl = 0;
int sumNegative = sumNegativeArray( array );
int sumPositive = sumPositiveArray( array );
array = selectionSortArray(array);
System.
out.
println( "Start Loop");
for( int i = 0; true;i++){
testNo = 0;
System.
out.
println("New Loop");
for( int j = 0; j < currentLvl ; j ++) {
testNo += array[indexTree[j]];
}
System.
out.
print( "Test no: " + testNo +
" ");
if( testNo == contains) {
System.
out.
println( "Answer found");
int[] answer = new int[currentLvl];
for( int j = 0; j < currentLvl; j++) {
answer[j] = indexTree[j];
}
return answer;
}
System.
out.
print( "AnswerNotFound: ");
if( testNo + sumNegative > contains || testNo + sumPositive < contains ) {
System.
out.
print("TooHighOrLowGoingDown: ");
currentLvl--;
if( currentLvl == -1) {
currentLvl++;
indexTree[currentLvl]++;
if( indexTree[currentLvl] > array.length ) {
int[] answer = {-1};
return answer;
}
}else indexTree[currentLvl]++;
//indexTree[ currentLvl]++;
if( indexTree[currentLvl] > array.length) {
printArray(indexTree);
while( indexTree[currentLvl] > array.length) {
System.
out.
print("EndofArrayGoingDown: ");
currentLvl--;
indexTree[currentLvl]++;
}
}
}
if( testNo + sumNegative <= contains || testNo + sumPositive >= contains ) {
System.
out.
print("ToosmallGoingUp: " );
indexTree[currentLvl + 1] = indexTree[currentLvl] +1;
currentLvl++;
System.
out.
println(currentLvl +
" " +array.
length);
while( indexTree[currentLvl] > array.length) {
System.
out.
print("EndofArrayGoingDown: " + indexTree
[currentLvl
]);
currentLvl--;
if( currentLvl == -1 ){
System.
out.
println("AnswerNotFoundTerminating");
int[] answer = {-1};
return answer;
}
indexTree[currentLvl]++;
}
}
printArray(indexTree);
}
}