The first line gives n and the second line gives n integers. Print the largest number and the position (counting from 1) where it first appears, separated by a space.
Input
1 ≤ n ≤ 1,000, -1,000,000 ≤ each number ≤ 1,000,000
Output
max position
Example 1 · stdin
5
3 9 2 9 1
stdout
9 2
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
If the maximum appears more than once, print the first position: compare with > — using >= gives the last one. Python scans twice with max() and index(); other languages scan once and remember the position. Add 1 to the 0-based index.
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let best = 0;for (let i = 1; i < n; i++) if (a[i] > a[best]) best = i;console.log(a[best], best + 1);
TypeScriptmain.ts · 5 lines
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let best: number = 0;for (let i = 1; i < n; i++) if (a[i] > a[best]) best = i;console.log(a[best], best + 1);
JavaMain.java · 13 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int best = 0; for (int i = 1; i < n; i++) if (a[i] > a[best]) best = i; System.out.println(a[best] + " " + (best + 1)); }}
C#Main.cs · 13 lines
using System;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); int best = 0; for (int i = 1; i < n; i++) if (a[i] > a[best]) best = i; Console.WriteLine($"{a[best]} {best + 1}"); }}
C++main.cpp · 12 lines
#include <algorithm>#include <iostream>#include <vector>int main() { int n; std::cin >> n; std::vector<int> a(n); for (int& x : a) std::cin >> x; int best = std::max_element(a.begin(), a.end()) - a.begin(); std::cout << a[best] << ' ' << best + 1 << '\n';}
Cmain.c · 11 lines
#include <stdio.h>int main(void) { int n, a[1000]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int best = 0; for (int i = 1; i < n; i++) if (a[i] > a[best]) best = i; printf("%d %d\n", a[best], best + 1); return 0;}
Craykonmain.cray · 11 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();best = 0;for (i = 1; i < n; i++){ if (a[i] > a[best]) { best = i; }}Console.WriteLine(a[best] + " " + (best + 1));