Created
February 4, 2020 03:43
-
-
Save TuHuynhVan/64a85a9ec7226ed77e9ac179005458dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package practice; | |
public class SumArray { | |
public static void main(String[] args) { | |
int[] arr_1 = { 1, 2, 3, 4 }; | |
System.out.println(sumArray(arr_1)); | |
} | |
public static int sumArray(int[] a) { | |
int initialIndex = a.length - 1; | |
return sumArrayResursion(a, initialIndex); | |
} | |
public static int sumArrayResursion(int[] a, int index) { | |
// That means we don;t need to add any element to the sum | |
if (index < 0) { | |
return 0; | |
} else { | |
return a[index] + sumArrayResursion(a, index - 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment