-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJune6
More file actions
29 lines (25 loc) · 752 Bytes
/
Copy pathJune6
File metadata and controls
29 lines (25 loc) · 752 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
class Solution {
public:
long long max_sum(int a[], int n) {
long long sum = 0;
long long tsum = 0;
// Ensure val is of type long long
// This way, val * a[i] will be evaluated as long long,
// preventing overflow that could occur if a[i] was multiplied by an int
long long val ;
for(int i = 0 ; i<n ;i++){
val = i;
sum+=a[i];
tsum+=val*a[i];
}
long long maxi = tsum;
//N*a[n-i] will not cause overflow
// n*a[n-i] can causes overflow
long long N = n;
for(int i = 1 ; i<n ;i++){
tsum = tsum + sum - N*a[n-i];
maxi = max(maxi,tsum);
}
return maxi;
}
};