Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let totalPairs = Number(input.shift());
- let currentPair = 0;
- let previousPair = 0;
- let currentDifference = 0;
- let maxDifference = 0;
- let equal = 1;
- for (let i = 0; i < totalPairs; i++) {
- let firstNum = Number(input.shift());
- let secondNum = Number(input.shift());
- let sum = firstNum+secondNum;
- currentPair = sum;
- if (currentPair === previousPair) {
- equal++;
- }
- if ((currentPair !== previousPair) && i > 0) {
- if (currentPair >= 0 && previousPair >= 0) {
- currentDifference = Math.max(currentPair, previousPair) - Math.min(currentPair, previousPair);
- } else {
- currentDifference = Math.max(currentPair, previousPair) - Math.min(currentPair, previousPair);
- }
- previousPair = currentPair;
- if (currentDifference > maxDifference) {
- maxDifference = currentDifference;
- }
- } else {
- previousPair = currentPair
- }
- }
- if (equal === totalPairs) {
- console.log(`Yes, value=${currentPair}`);
- } else {
- console.log(`No, maxdiff=${maxDifference}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment