Skip to content

Instantly share code, notes, and snippets.

@xtea
Last active August 30, 2019 08:38
Show Gist options
  • Save xtea/1f0a47aa718b728fc0b0e190e7737184 to your computer and use it in GitHub Desktop.
Save xtea/1f0a47aa718b728fc0b0e190e7737184 to your computer and use it in GitHub Desktop.
TapeEquilibrium - Codility
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int size = A.length;
int[] presum = new int[size];
presum[size - 1] = A[size - 1];
for (int i = A.length - 2; i >= 0; i --) {
presum[i] = presum[i + 1] + A[i];
}
//
int ans = Integer.MAX_VALUE;
int leftSum = A[0];
for (int i = 1; i < A.length; i ++) {
int diff = Math.abs(leftSum - presum[i]);
ans = Math.min(ans, diff);
leftSum += A[i];
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment