Advertisement
DPELED

MAMAN14_2018a_3

Jan 5th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1.     public static boolean isWay(int[] arr)
  2.     {
  3.         return isWay(arr, 0);
  4.     }
  5.  
  6.     private static boolean isWay(int[] arr, int i)
  7.     {
  8.         final int VISITED=0;
  9.         if(i<0||i>=arr.length)//if got out from the array limits
  10.             return false;
  11.         if(i==arr.length-1)//if got to the end of the array
  12.             return true;
  13.         if(arr[i]==VISITED)//if already visited
  14.             return false;
  15.  
  16.         int temp=arr[i];//saving the value to set it back later
  17.         arr[i]=VISITED;//making a flag of visited value;
  18.         boolean result= isWay(arr, i-temp)||isWay(arr,i+temp);//going to left and right in distace of the value store in the current index
  19.         arr[i]=temp;//setting back the original value
  20.         return result;
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement