-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray2.py
43 lines (34 loc) · 1.13 KB
/
array2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.
# Note: If the second largest element does not exist, return -1.
# Examples:
# Input: arr[] = [12, 35, 1, 10, 34, 1]
# Output: 34
# Explanation: The largest element of the array is 35 and the second largest element is 34.
# Input: arr[] = [10, 5, 10]
# Output: 5
# Explanation: The largest element of the array is 10 and the second largest element is 5.
# Input: arr[] = [10, 10, 10]
# Output: -1
# Explanation: The largest element of the array is 10 there is no second largest element.
#time - O(n*logn)
def bruteforce(arr):
n = len(arr)
arr.sort()
for i in range(n-2,-1,-1):
if arr[n-1] != arr[i]:
return arr[i]
return -1
#time - O(n)
def optimalsoln(arr):
largest = -1
seclargest = -1
n = len(arr)
for i in range(n):
if arr[i] > largest:
seclargest = largest
largest = arr[i]
elif arr[i] < largest and arr[i] > seclargest:
seclargest = arr[i]
return seclargest
arr = list(map(int,input().split()))
print(optimalsoln(arr))