-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArraySum_Recursive.java
More file actions
29 lines (23 loc) · 989 Bytes
/
ArraySum_Recursive.java
File metadata and controls
29 lines (23 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package Week_2;
public class ArraySum_Recursive {
// Program to recursively calculate the sum of elements in an array.
// Recursive formula:
// Sum of elements from position i to the end of array
// = (element at position i) + (sum of elements from position i + 1 to the end of the array)
// OR
// findSum(position) = ar[position] + findSum(position + 1)
// Base Case:
// our position is at the end of the array - There are no more elements after it so we simply return the
// value of the element
static int findSum(int pos, int[] ar) {
//End of array
if (pos == ar.length - 1) return ar[pos];
// Sum = current position + sum of rest of array (rest of array is from pos+1 to the end)
return ar[pos] + findSum(pos + 1, ar);
}
public static void main(String[] args) {
int[] ar = {1, 2, 3, 4, 5, 6};
int sum = findSum(0, ar);
System.out.println("Sum: " + sum);
}
}