title: Small exercise for Java given two arrays which represents a path
body: <p>Given two arrays which represents a path and each element in the array represent the time it takes the driver to travel, write a method that chooses the fastest path he can take. The driver can switch paths only once between arrays.
  For example the following arrays:</p>
  <pre><code>int[] road1 = new int[] { 5, 4, 5, 8, 12, 9, 9, 3 };
  int[] road2 = new int[] { 7, 3, 3, 12, 10, 2, 10, 7 };
  </code></pre>
  <p>the output should be 49 since the driver will start at road2 and switch at index 6 to the second Array.</p>
  <p>Edit:
  My question is how do I make the recursion stop after switching to the other array? I tried to put a counter marker but it didn't work and I reverted back to my original output. Am I missing something about how recursion works?</p>
  <p>My code prints 53 where it should print 49.</p>
  <p>My code:</p>
  <pre><code>public class MyClass {
  
      public static int shortestRoad(int[] road1, int[] road2) {
          return shortestRoadNumbers(road1, road2, 0);
      }
  
      private static int shortestRoadNumbers(int[] road1, int[] road2, int index) {
          if (index == road1.length || index == road2.length) {
              return 0;
          }
          if (road1[index] &gt;= road2[index] &amp;&amp; road1[index + 2] &gt;= road2[index + 2]) {
              return (road2[index] + shortestRoadNumbers(road1, road2, index + 1));
          } else {
              return (road1[index] + shortestRoadNumbers(road1, road2, index + 1));
          }
  
      }
  
      public static void main(String args[]) {
          int[] road1 = new int[] { 5, 4, 5, 8, 12, 9, 9, 3 };
          int[] road2 = new int[] { 7, 3, 3, 12, 10, 2, 10, 7 };
          MyClass.shortestRoad(road1, road2);
          int result = MyClass.shortestRoad(road1, road2);
          System.out.println(result);
      }
  }
  </code></pre>