1. public static int[] existsInSortedSubset(int[] array, int contains) {
  2. int testNo;
  3. int[] indexTree = new int[array.length +2];
  4. int currentLvl = 0;
  5. int sumNegative = sumNegativeArray( array );
  6. int sumPositive = sumPositiveArray( array );
  7. array = selectionSortArray(array);
  8. System.out.println( "Start Loop");
  9. for( int i = 0; true;i++){
  10. testNo = 0;
  11. System.out.println("New Loop");
  12. for( int j = 0; j < currentLvl ; j ++) {
  13. testNo += array[indexTree[j]];
  14. }
  15. System.out.print( "Test no: " + testNo + " ");
  16. if( testNo == contains) {
  17. System.out.println( "Answer found");
  18. int[] answer = new int[currentLvl];
  19. for( int j = 0; j < currentLvl; j++) {
  20. answer[j] = indexTree[j];
  21. }
  22. return answer;
  23. }
  24. System.out.print( "AnswerNotFound: ");
  25. if( testNo + sumNegative > contains || testNo + sumPositive < contains ) {
  26. System.out.print("TooHighOrLowGoingDown: ");
  27. currentLvl--;
  28. if( currentLvl == -1) {
  29. currentLvl++;
  30. indexTree[currentLvl]++;
  31. if( indexTree[currentLvl] > array.length ) {
  32. int[] answer = {-1};
  33. return answer;
  34. }
  35. }else indexTree[currentLvl]++;
  36. //indexTree[ currentLvl]++;
  37. if( indexTree[currentLvl] > array.length) {
  38. System.out.print("test");
  39. printArray(indexTree);
  40. while( indexTree[currentLvl] > array.length) {
  41. System.out.print("EndofArrayGoingDown: ");
  42. currentLvl--;
  43. indexTree[currentLvl]++;
  44. }
  45. }
  46. }
  47.  
  48. if( testNo + sumNegative <= contains || testNo + sumPositive >= contains ) {
  49. System.out.print("ToosmallGoingUp: " );
  50. indexTree[currentLvl + 1] = indexTree[currentLvl] +1;
  51. currentLvl++;
  52. System.out.println(currentLvl + " " +array.length);
  53. while( indexTree[currentLvl] > array.length) {
  54. System.out.print("EndofArrayGoingDown: " + indexTree[currentLvl]);
  55. currentLvl--;
  56. if( currentLvl == -1 ){
  57. System.out.println("");
  58. System.out.println("AnswerNotFoundTerminating");
  59. int[] answer = {-1};
  60. return answer;
  61. }
  62. indexTree[currentLvl]++;
  63. }
  64. }
  65. printArray(indexTree);
  66. }
  67. }