Second Largest Distinct (Lists)
Python
Medium
3 views
Problem Description
Read n integers. Output the second largest distinct value, or NONE if not exists.
Input Format
First line n. Second line n integers.
Output Format
Second largest distinct or NONE.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=list(map(int,p[1:1+n]))
first=None
second=None
for v in a:
if first is None or v>first:
if first is None or v!=first:
second=first
first=v
elif v!=first and (second is None or v>second):
second=v
sys.stdout.write('NONE' if second is None else str(second))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!