The first line gives the count n, and the second line gives n integers. Print the second largest among the distinct values. A value that appears several times counts once (for 5 5 3 the answer is 3). If there is only one distinct value, print "none".
Input
1 ≤ n ≤ 100, -1000 ≤ each integer ≤ 1000
Output
the second largest value, or "none"
Example 1 · stdin
5
2 9 9 9 1
stdout
2
Example 2 · stdin
3
4 4 4
stdout
none
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
The most common trap is a repeated maximum: sorting and taking the second from the end (sorted(a)[-2]) gives 5 for 5 5 3. The second trap is the starting value: starting at 0 fails when every number is negative, so start from the first value (or INT_MIN in C). Removing repeats: Python keeps only values below the maximum and takes max, C# uses Distinct().OrderByDescending(), C++ puts them in a std::set and reads std::next(s.rbegin()). C and Java make one pass, updating the largest and second largest together. Craykon finds the largest with foreach (x in a), then makes another pass for the largest value with x < top.
Pythonmain.py · 5 lines
n = int(input())a = list(map(int, input().split()))top = max(a)rest = [x for x in a if x < top]print(max(rest) if rest else "none")
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const top: number = Math.max(...a);const rest: number[] = a.filter((x) => x < top);console.log(rest.length > 0 ? String(Math.max(...rest)) : "none");
JavaMain.java · 23 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 first = sc.nextInt(); int second = 0; boolean hasSecond = false; for (int i = 1; i < n; i++) { int x = sc.nextInt(); if (x > first) { second = first; hasSecond = true; first = x; } else if (x < first && (!hasSecond || x > second)) { second = x; hasSecond = true; } } System.out.println(hasSecond ? String.valueOf(second) : "none"); }}
#include <iostream>#include <iterator>#include <set>int main() { int n, x; std::cin >> n; std::set<int> values; for (int i = 0; i < n; i++) { std::cin >> x; values.insert(x); } if (values.size() < 2) std::cout << "none\n"; else std::cout << *std::next(values.rbegin()) << "\n";}
Cmain.c · 20 lines
#include <stdio.h>int main(void) { int n, x, first, second = 0, has_second = 0; scanf("%d %d", &n, &first); for (int i = 1; i < n; i++) { scanf("%d", &x); if (x > first) { second = first; has_second = 1; first = x; } else if (x < first && (!has_second || x > second)) { second = x; has_second = 1; } } if (has_second) printf("%d\n", second); else puts("none"); return 0;}
Craykonmain.cray · 25 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();top = a[0];foreach (x in a){ top = Mathf.Max(top, x);}found = false;second = 0;foreach (x in a){ if (x < top && (!found || x > second)) { second = x; found = true; }}if (found){ Console.WriteLine(second);}else{ Console.WriteLine("none");}