// Best Index // Sample code to perform I/O: process.stdin.resume(); process.stdin.setEncoding("utf-8"); var stdin_input = ""; process.stdin.on("data", function (input) { stdin_input += input; // Reading input from STDIN }); process.stdin.on("end", function () { main(stdin_input); }); function main(input) { var data= input.toString().split("\n"); //process.stdout.write(bestIndex(data[1], parseInt(data[0]))); console.log(bestIndex(data[1], parseInt(data[0]))); // Writing output to STDOUT } // Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail // Write your code here // Sample code to perform I/O: //Enter your code here function bestIndex(input,n) { let arr = input.split(" ").map((item)=>Number(item)); let max = -Infinity; for (let i = 0; i<n; i++) { if (i>0) arr[i] = arr[i]+arr[i-1]; } for (let j = 0; j<n; j++) { let left = n- j; let sum =0; let k =Math.trunc(((-1+Math.sqrt((8*left+1)))/2)); sum=arr[Math.trunc((k*(k+1))/2+j-1)]; if (j!=0) {sum-=arr[j-1];} if (max<sum) {max=sum;} } return max.toString(); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter