-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSum of Array.txt
43 lines (36 loc) · 854 Bytes
/
Sum of Array.txt
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Sum Of Array
Send Feedback
Given an array of length N, you need to find and return the sum of all elements of the array.
Do this recursively.
Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Output Format :
Sum
Constraints :
1 <= N <= 10^3
Sample Input 1 :
3
9 8 9
Sample Output 1 :
26
Sample Input 2 :
3
4 2 1
Sample Output 2 :
7
//////////////////---------------------------->>>>>>>>>>>>>>>>>>>>>>>>>
public class Solution {
public static int sum(int input[]) {
if(input.length == 0)
return -1;
if(input.length == 1)
return input[0];
int []smallArray = new int[input.length - 1];
for(int i = 1; i < input.length; i++)
smallArray[i-1] = input[i];
int total = sum(smallArray);
total+=input[0];
return total;
}
}