Advertisement
Spocoman

09. Left and Right Sum

Dec 23rd, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function leftAndRightSum(input) {
  2.  
  3.     let num = parseInt(input[0]);
  4.     let leftSum = 0;
  5.     let rightSum = 0;
  6.  
  7.     for (let i = 1; i <= num * 2; i++) {
  8.         if (i <= num){
  9.             leftSum += parseInt(input[i]);
  10.         }
  11.         else{
  12.             rightSum += parseInt(input[i]);
  13.         }
  14.     }
  15.  
  16.     if (leftSum === rightSum) {
  17.         console.log(`Yes, sum = ${leftSum}`);
  18.     } else {
  19.         console.log(`No, diff = ${Math.abs(leftSum - rightSum)}`);
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement