/*
* Here i have declared initial input which we discussed over whatsapp which would be as below.
* For now i have added \n into string to prove the concept, and test faster.
*/
const inputString = "10 10\n2\n7\n5\n9\n6\n1\n8\n10\n3\n4";
/*
* Next two line is to split and make input appropiate for next process.
*/
let originalInputArr = inputString.split("\n");
let inputSet = originalInputArr[0].split(' ');
/*
* Set initial values for N and M. For first set.
*/
let N = inputSet[0];
let M = inputSet[1];
let resultArr = [];
const processGoodRange = (input, N) =>
{
resultArr.push(parseInt(input));
resultArr.sort((a,b) => a - b);
// console.log("A: => "+ a);
console.log(resultArr);
let a = new Set([...resultArr]);
resultArr = Array.from(a);
let sum = 0;
for (let i= resultArr.length-1; i>=0; i--) {
sum += parseInt(resultArr[i-1] ? resultArr[i-1] + 1 : 1) + parseInt( resultArr[i+1] ? resultArr[i+1] - 1 : N);
}
console.log(sum);
}
for (let i = 1; i < originalInputArr.length; i++) {
processGoodRange(originalInputArr[i], N)
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.