Second Largest Distinct
Python
Medium
3 views
Problem Description
Read n integers, find the second largest DISTINCT number. If it doesn't exist, output NONE.
Input Format
Line1: n. Line2: 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])
arr=list(map(int,p[1:1+n]))
first=None
second=None
for v in arr:
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!