Advertisement
Btwonu

Left and Right Sum

Feb 29th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function maxima(para) {
  2. //Input
  3.     let n = +para[0];
  4.     let leftColumnSum = 0;
  5.     let rightColumnSum = 0;
  6.    
  7. //Code
  8.  
  9. //leftColumn
  10.     for(let i = 1; i <= n; i++) {
  11.         leftColumnSum += parseInt(para[i]);
  12.        
  13.     }
  14.    
  15. //rightColumn
  16.     for(let i = para.length - 1; i > n; i--) {
  17.         rightColumnSum += parseInt(para[i]);
  18.        
  19.     }
  20.  
  21. //Difference  
  22.     let difference = Math.abs(leftColumnSum - rightColumnSum);
  23.  
  24. //Output
  25.     if(leftColumnSum === rightColumnSum) {
  26.         console.log(`Yes, sum = ${leftColumnSum}`);
  27.     } else {
  28.         console.log(`No, diff = ${difference}`);
  29.     }
  30. }
  31.  
  32. maxima([
  33.     '2',
  34.     '10',
  35.     '90',
  36.     '60',
  37.     '40',
  38. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement