georgiev955

Task 8

Mar 2nd, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let totalPairs = Number(input.shift());
  3.     let currentPair = 0;
  4.     let previousPair = 0;
  5.     let currentDifference = 0;
  6.     let maxDifference = 0;
  7.     let equal = 1;
  8.    
  9.  
  10.     for (let i = 0; i < totalPairs; i++) {
  11.         let firstNum = Number(input.shift());
  12.         let secondNum = Number(input.shift());
  13.         let sum = firstNum+secondNum;
  14.         currentPair = sum;
  15.         if (currentPair === previousPair) {
  16.             equal++;
  17.         }
  18.         if ((currentPair !== previousPair) && i > 0) {
  19.             if (currentPair >= 0 && previousPair >= 0) {
  20.                 currentDifference = Math.max(currentPair, previousPair) - Math.min(currentPair, previousPair);
  21.             } else {
  22.                 currentDifference = Math.max(currentPair, previousPair) - Math.min(currentPair, previousPair);
  23.             }
  24.             previousPair = currentPair;
  25.             if (currentDifference > maxDifference) {
  26.                 maxDifference = currentDifference;
  27.             }
  28.         } else {
  29.             previousPair = currentPair
  30.         }
  31.     }
  32.     if (equal === totalPairs) {
  33.         console.log(`Yes, value=${currentPair}`);
  34.     } else {
  35.         console.log(`No, maxdiff=${maxDifference}`);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment