Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. public int solution(int[] A) {
  2. int i = 0;
  3. int j = A.length -1;
  4. int left = A[i];
  5. int right = A[j];
  6.  
  7. if(A.length == 0) {
  8. return 0;
  9. }else if(A.length == 1) {
  10. return A[0];
  11. }else if(A.length == 2) {
  12. return Math.abs(A[0] - A[1]);
  13. }
  14.  
  15.  
  16. while(i < j -1){
  17. if(Math.abs(left) < Math.abs(right)){
  18. i++;
  19. left += A[i];
  20. }else if(Math.abs(right) < Math.abs(left)){
  21. j--;
  22. right += A[j];
  23. }else{
  24. i++;
  25. left += A[i];
  26. }
  27. }
  28. return Math.abs(Math.abs(left) - Math.abs(right));
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement