-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAVeryBigSum.cs
70 lines (51 loc) · 1.83 KB
/
AVeryBigSum.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.
// Function Description
// Complete the function aVeryBigSum described below to return the sum of all elements of the array.
// aVeryBigSum(integer: n, integer: arr_size, integer array: arr)
// Parameters:
// n: array size
// arr_size: array size
// ar: array of integers to sum
// Returns: integer sum of all array elements
// Constraints
// Raw Input Format
// The first line of the input consists of an integer .
// The next line contains space-separated integers contained in the array.
// Sample Input 0
// 5
// 1000000001 1000000002 1000000003 1000000004 1000000005
// Sample Output 0
// 5000000015
// Note:
// The range of the 32-bit integer is .
// When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
/*
* Complete the aVeryBigSum function below.
*/
static long aVeryBigSum(int n, long[] ar) {
/*
* Write your code here.
*/
long sum = 0;
foreach (var element in ar) {
Console.Out.WriteLine(element);
sum += element;
}
return sum;
}
static void Main(string[] args) {
TextWriter tw = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
long[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt64(arTemp))
;
long result = aVeryBigSum(n, ar);
tw.WriteLine(result);
tw.Flush();
tw.Close();
}
}