Skip to content

Instantly share code, notes, and snippets.

@TuHuynhVan
Created February 4, 2020 03:43
Show Gist options
  • Save TuHuynhVan/64a85a9ec7226ed77e9ac179005458dc to your computer and use it in GitHub Desktop.
Save TuHuynhVan/64a85a9ec7226ed77e9ac179005458dc to your computer and use it in GitHub Desktop.
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