Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. function processData(input) {
  2. var lines = input.split('\n');
  3. for (var i=2; i<lines.length; i+=2) {
  4. var arr = lines[i].split(' ').map(function(x) { return parseInt(x); });
  5.  
  6. if (arr.length > 1) {
  7. var sum = 0;
  8.  
  9. // First, get the sum of the entire array.
  10. for (var j=0; j<arr.length; j++) {
  11. sum += arr[j];
  12. }
  13.  
  14. // Now step through using each value as a test index for left and right sums.
  15. // We can add and subtract at each step to change the left and right sums accordingly.
  16. var sumLeft = 0;
  17. var sumRight = 0;
  18. for (var j=1; j<arr.length - 1; j++) {
  19. sumLeft += arr[j-1];
  20. sumRight = sum - sumLeft - arr[j];
  21.  
  22. if (sumLeft === sumRight) {
  23. break;
  24. }
  25. }
  26.  
  27. if (j < arr.length - 1) {
  28. console.log('YES');
  29. }
  30. else {
  31. console.log('NO');
  32. }
  33. }
  34. else {
  35. // Sum considered to be 0 for a single element array.
  36. console.log('YES');
  37. }
  38. }
  39. }
  40.  
  41. process.stdin.resume();
  42. process.stdin.setEncoding("ascii");
  43. _input = "";
  44. process.stdin.on("data", function (input) {
  45. _input += input;
  46. });
  47.  
  48. process.stdin.on("end", function () {
  49. processData(_input);
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement