0
0

More than 1 year has passed since last update.

paizaラーニング レベルアップ問題集 線形探索メニュー応用編 JavaScript 連続する 3 要素

Posted at

連続する 3 要素 (paizaランク C 相当)

解答例(for,if)

forとifを使って解きます。

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf-8").trim();
const lines = input.split("\n");

const n = Number(lines[0]);
const a = lines[1].split(" ").map(Number);

let ans = 0;//最も小さいi
let max = -Infinity;//最大値
for (let i = 0; i <= n - 3; i++) {
  const sum = a[i] + a[i + 1] + a[i + 2];
  if (sum > max) { //もっとも小さい i を答えるので > 
    max = sum;
    ans = i + 1;
  }
}
console.log(ans);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0