Longest abs Array

Run Settings
LanguageJavaScript
Language Version
Run Command
'use strict'; const fs = require('fs'); process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', function(inputStdin) { inputString += inputStdin; }); process.stdin.on('end', function() { inputString = inputString.split('\n'); main(); }); function readLine() { return inputString[currentLine++]; } /* * Complete the 'pickingNumbers' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY a as parameter. */ function pickingNumbers(a) { // Write your code here a.sort(); let arr = [] let count ; for(let i = 0 ; i < a.length ; i++){ count = 0 for(let j = i +1 ; j< a.length; j++){ if(Math.abs(a[i]-a[j]) <= 1){ count++ } } arr.push(count) } return Math.max(...arr) +1 } function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH); const n = parseInt(readLine().trim(), 10); const a = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10)); const result = pickingNumbers(a); ws.write(result + '\n'); ws.end(); }
Editor Settings
Theme
Key bindings
Full width
Lines