-
Notifications
You must be signed in to change notification settings - Fork 25
/
TapeEquilibrium.cpp
38 lines (31 loc) · 949 Bytes
/
TapeEquilibrium.cpp
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
// https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(N)
//
#include <limits>
#include <cmath>
int solution(vector<int> &A) {
vector<long long> sums;
unsigned int N = A.size();
long long sum = 0;
// generate all of the possible sums
for (unsigned int i=0; i<N; i++) {
sum += A[i];
sums.push_back(sum);
}
// figure out which difference is the smallest
long long minDiff = std::numeric_limits<long long>::max();
// Can't do the entire array, have to stop at one less than the max
for (unsigned int i=0; i<(N-1); i++) {
// Simplifying this:
// sums[i] - (sums[i] - sum) = 2*sums[i]-sum;
long long diff = abs( 2*sums[i] - sum );
if ( diff < minDiff ) {
minDiff = diff;
}
}
return minDiff;
}