Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int solution(int[] A) {
- int i = 0;
- int j = A.length -1;
- int left = A[i];
- int right = A[j];
- if(A.length == 0) {
- return 0;
- }else if(A.length == 1) {
- return A[0];
- }else if(A.length == 2) {
- return Math.abs(A[0] - A[1]);
- }
- while(i < j -1){
- if(Math.abs(left) < Math.abs(right)){
- i++;
- left += A[i];
- }else if(Math.abs(right) < Math.abs(left)){
- j--;
- right += A[j];
- }else{
- i++;
- left += A[i];
- }
- }
- return Math.abs(Math.abs(left) - Math.abs(right));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement