-
Notifications
You must be signed in to change notification settings - Fork 36
/
SumofArray
38 lines (32 loc) · 900 Bytes
/
SumofArray
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
import java.util.Scanner;
public class Runner {
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
int n = s.nextInt();
int input[] = new int[n];
for(int i = 0; i < n; i++) {
input[i] = s.nextInt();
}
System.out.println(Solution.sum(input));
}
}
public class Solution {
public static int sum(int input[]) {
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
if(input.length==1){
return input[0];
}
int smallerInput[]=new int[input.length-1];
for(int index=1;index<input.length;index++){
smallerInput[index-1]=input[index];
}
int total=sum(smallerInput);
total+=input[0];
return total;
}
}