Given an array Arr of size N, print second largest distinct element from an array.
Example 1:
Input: 
N = 6
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.
Example 2:
Input: 
N = 3
Arr[] = {10, 5, 10}
Output: 5
Explanation: The largest element of 
the array is 10 and the second 
largest element is 5.
Your Task:
You don't need to read input or print anything. Your task is to complete the function print2largest() which takes the array of integers arr and n as parameters and returns an integer denoting the answer. If 2nd largest element doesn't exist then return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
2 ≤ N ≤ 10^{5}
1 ≤ Arr_{i }≤ 10^{5}

Difficulty: EASY
Tags: Data structures

Complete search
Skill types: Data structures

Complete search
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:



	def print2largest(self,arr, n):

		# code here

Solutions:
class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			return -1
		(largest, second_largest) = (float('-inf'), float('-inf'))
		for i in range(n):
			if arr[i] > largest:
				second_largest = largest
				largest = arr[i]
			elif arr[i] > second_largest and arr[i] != largest:
				second_largest = arr[i]
		if second_largest == float('-inf'):
			return -1
		else:
			return second_largest


class Solution:

	def print2largest(self, arr, n):
		if arr[0] > arr[1]:
			max = arr[0]
			second = arr[1]
		elif arr[0] == arr[1]:
			max = arr[0]
			second = -1
		else:
			second = arr[0]
			max = arr[1]
		for x in arr:
			if x > max:
				second = max
				max = x
			if second < x and x != max:
				second = x
		return second


class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			return -1
		(l1, l2) = (arr[0], -1)
		for i in arr[1:]:
			if i > l1:
				l2 = l1
				l1 = i
			elif i > l2 and i != l1:
				l2 = i
		return l2


class Solution:

	def print2largest(self, arr, n):
		pastMax = -1
		max = arr[0]
		for x in range(0, len(arr)):
			if arr[x] > max:
				pastMax = max
				max = arr[x]
			if arr[x] == max:
				continue
			if arr[x] > pastMax:
				pastMax = arr[x]
		return pastMax


class Solution:

	def print2largest(self, arr, n):
		lar = arr[0]
		seclar = -1
		for i in arr:
			if i > lar:
				lar = i
		for j in arr:
			if j < lar and j > seclar:
				seclar = j
		return seclar


class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			return -1
		first = second = float('-inf')
		for i in range(n):
			if arr[i] > first:
				second = first
				first = arr[i]
			elif arr[i] > second and arr[i] != first:
				second = arr[i]
		if second == float('-inf'):
			return -1
		else:
			return second
		return second_max_value


class Solution:

	def print2largest(self, arr, n):
		arr.sort()
		arr = arr[::-1]
		for i in range(n - 1):
			if arr[i] > arr[i + 1]:
				return arr[i + 1]
		return -1


class Solution:

	def print2largest(self, arr, n):
		largest = max(arr)
		sl = -9999
		for ele in arr:
			if ele != largest:
				if ele >= sl:
					sl = ele
		if sl == -9999:
			return -1
		else:
			return sl


class Solution:

	def print2largest(self, arr, n):
		large = second = -232
		s = set(arr)
		if len(s) == 1:
			return -1
		for i in range(n):
			large = max(large, arr[i])
		for i in range(n):
			if arr[i] != large:
				second = max(second, arr[i])
		return second


class Solution:

	def print2largest(self, arr, n):
		s = set(arr)
		if len(s) == 1:
			return -1
		s = sorted(s)
		return s[-2]


class Solution:

	def print2largest(self, arr, n):
		k = set(arr)
		m = list(k)
		m.sort()
		k = len(m)
		if len(m) == 1:
			return -1
		else:
			return m[k - 2]


class Solution:

	def print2largest(self, arr, n):
		slarg = -1
		larg = arr[0]
		for i in range(1, n):
			if arr[i] > larg:
				slarg = larg
				larg = arr[i]
			if arr[i] < larg and arr[i] > slarg:
				slarg = arr[i]
		return slarg


class Solution:

	def print2largest(self, arr, n):
		x = list(set(arr))
		if len(x) > 1:
			x.sort()
			return x[-2]
		else:
			return -1


class Solution:

	def print2largest(self, arr, n):
		largest = arr[0]
		slargest = -1
		for i in range(n):
			if largest < arr[i]:
				slargest = largest
				largest = arr[i]
			elif arr[i] < largest:
				if arr[i] > slargest:
					slargest = arr[i]
		return slargest


class Solution:

	def print2largest(self, arr, n):
		largest = arr[0]
		second_largest = -1
		for num in arr:
			if num > largest:
				second_largest = largest
				largest = num
			elif num > second_largest and num < largest:
				second_largest = num
		return second_largest


class Solution:

	def print2largest(self, arr, n):
		distinct_elements = set(arr)
		distinct_arr = list(distinct_elements)
		distinct_arr.sort(reverse=True)
		if len(distinct_arr) < 2:
			return -1
		else:
			return distinct_arr[1]


class Solution:

	def print2largest(self, arr, n):
		max1 = -1
		max2 = -1
		if n < 2:
			return -1
		for i in range(n):
			if arr[i] > max1:
				max2 = max1
				max1 = arr[i]
			elif arr[i] > max2 and arr[i] != max1:
				max2 = arr[i]
		return max2


class Solution:

	def print2largest(self, arr, n):
		first = -1
		second = -1
		for i in arr:
			if i > first:
				second = first
				first = i
			elif i > second and i != first:
				second = i
		return second


class Solution:

	def print2largest(self, arr, n):
		flag = False
		arr.sort()
		max = arr[n - 1]
		for i in range(n - 2, -1, -1):
			if arr[i] == max:
				flag = False
			else:
				flag = True
				max = arr[i]
				break
		if flag:
			return max
		else:
			return -1


class Solution:

	def print2largest(self, arr, n):
		l = sl = -2147483648
		for i in range(n):
			if arr[i] > l:
				sl = l
				l = arr[i]
			elif arr[i] > sl and arr[i] != l:
				sl = arr[i]
		if sl == -2147483648:
			return -1
		else:
			return sl


class Solution:

	def print2largest(self, arr, n):
		arr.sort(reverse=True)
		first = arr[0]
		if n == 1:
			return -1
		i = 1
		while i < n and arr[i] == first:
			i += 1
		if i == n:
			return -1
		return arr[i]


class Solution:

	def print2largest(self, nums, n):
		if len(nums) < 2:
			return None
		max_val = second_max_val = float('-inf')
		for num in nums:
			if num > max_val:
				max_val = num
		for num in nums:
			if num != max_val and num > second_max_val:
				second_max_val = num
		return second_max_val if second_max_val != float('-inf') else -1


class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			return -1
		large = -1
		sec_large = -1
		for i in range(n):
			if arr[i] > large:
				sec_large = large
				large = arr[i]
			elif arr[i] > sec_large and arr[i] != large:
				sec_large = arr[i]
		return sec_large


class Solution:

	def print2largest(self, arr, n):
		if n > 1:
			arr.sort()
			a = max(arr)
			while a in arr:
				arr.pop()
			if len(arr) == 0:
				return -1
			else:
				return max(arr)
		else:
			return -1


class Solution:

	def print2largest(self, arr, n):
		arr = set(arr)
		if len(arr) > 1:
			arr.remove(max(arr))
			return max(arr)
		else:
			return -1


class Solution:

	def print2largest(self, arr, n):
		my_set = set(arr)
		other = list(my_set)
		other.sort(reverse=True)
		if len(other) == 1:
			return -1
		return other[1]


class Solution:

	def print2largest(self, arr, n):
		maxi = arr[0]
		smaxi = -float('INF')
		for i in range(1, len(arr)):
			if arr[i] > maxi:
				smaxi = maxi
				maxi = arr[i]
			elif arr[i] > smaxi and arr[i] != maxi:
				smaxi = arr[i]
		if smaxi == -float('INF'):
			return -1
		return smaxi


class Solution:

	def print2largest(self, arr, n):
		maxi = max(arr)
		res = -float('INF')
		for i in arr:
			if i != maxi:
				res = max(res, i)
		if res == -float('INF'):
			return -1
		return res


class Solution:

	def print2largest(self, arr, n):
		(maxi, secondmaxi) = (-1, -1)
		for i in arr:
			if i > maxi:
				secondmaxi = maxi
				maxi = i
			elif i > secondmaxi and i != maxi:
				secondmaxi = i
		return secondmaxi


class Solution:

	def print2largest(self, arr, n):
		arr.sort(reverse=True)
		a = arr[0]
		if arr[0] == arr[-1]:
			return -1
		for i in arr:
			if i != a:
				t = i
				break
		return t


class Solution:

	def print2largest(self, arr, n):
		b = sorted(set(arr))
		if len(b) <= 1:
			return -1
		else:
			return b[-2]


class Solution:

	def print2largest(self, arr, n):
		maxx = 0
		sm = 0
		for i in arr:
			if i > maxx:
				sm = maxx
				maxx = i
			elif maxx > i and i > sm:
				sm = i
		return sm if sm != 0 else -1


class Solution:

	def print2largest(self, arr, n):
		maxx = max(arr)
		sm = -1
		for i in arr:
			if i > maxx:
				maxx = i
			if maxx > i and i > sm:
				sm = i
		return sm


class Solution:

	def print2largest(self, arr, n):
		arr.sort()
		largest = arr[n - 1]
		second_largest = -1
		for i in range(n - 1, -1, -1):
			if arr[i] != largest:
				second_largest = arr[i]
				break
		return second_largest


class Solution:

	def print2largest(self, arr, n):
		max = -1
		min = -1
		for i in arr:
			if i > max:
				min = max
				max = i
			if i > min:
				if i != max:
					min = i
		return min


class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			print('Invalid')
			return
		first = second = -2147483648
		for i in range(n):
			if arr[i] > first:
				second = first
				first = arr[i]
			elif arr[i] > second and arr[i] != first:
				second = arr[i]
		if second == -2147483648:
			return -1
		else:
			return second


class Solution:

	def print2largest(self, arr, n):
		a1 = list(set(arr))
		a1.sort()
		i = 0
		j = n - 1
		if arr[i] == arr[j]:
			return -1
			i += 1
			j -= 1
		else:
			return a1[-2]


class Solution:

	def print2largest(self, arr, n):
		ans = -1
		f_large = max(arr)
		for i in arr:
			if i > ans and i != f_large:
				ans = i
		return ans


class Solution:

	def print2largest(self, arr, n):
		j = 0
		largest = -1
		secondlargest = -1
		if n > 1:
			for i in arr:
				if j < n:
					if i > largest:
						secondlargest = largest
						largest = i
					if i < largest and i > secondlargest:
						secondlargest = i
				j = j + 1
			return secondlargest


class Solution:

	def print2largest(self, arr, n):
		arr1 = list(sorted(set(arr)))
		return arr1[len(arr1) - 2] if len(arr1) > 1 else -1


class Solution:

	def print2largest(self, arr, n):
		maxi = arr[0]
		if n == 1:
			return -1
		second_maxi = -1
		for i in range(1, n):
			if arr[i] > maxi:
				second_maxi = maxi
				maxi = arr[i]
			elif arr[i] < maxi and arr[i] > second_maxi:
				second_maxi = arr[i]
		return second_maxi


class Solution:

	def print2largest(self, arr, n):
		arr.sort(reverse=True)
		m = arr[0]
		for i in range(1, n):
			if arr[i] != m:
				return arr[i]
		return -1


class Solution:

	def print2largest(self, arr, n):
		(large, slarge) = (arr[0], -1)
		for i in range(1, len(arr)):
			if arr[i] > large:
				slarge = large
				large = arr[i]
			elif arr[i] < large and arr[i] > slarge:
				slarge = arr[i]
		return slarge


class Solution:

	def print2largest(self, arr, n):
		arr.sort(reverse=True)
		num = arr[0]
		if arr[1] == False:
			return -1
		for i in arr:
			if i != num:
				return i
				break
		else:
			return -1


class Solution:

	def print2largest(self, arr, n):
		k = max(arr)
		m = -1
		for i in range(n):
			if arr[i] > m and arr[i] < k:
				m = arr[i]
		return m


class Solution:

	def print2largest(self, arr, n):
		arr.sort()
		max = arr[-1]
		for num in reversed(range(len(arr) - 1)):
			if max != arr[num]:
				return arr[num]
			else:
				continue
		return -1


class Solution:

	def print2largest(self, arr, n):
		max1 = -1
		for i in arr:
			if i >= max1:
				max1 = i
		max2 = -1
		for i in arr:
			if i >= max2 and i < max1:
				max2 = i
		return max2


class Solution:

	def print2largest(self, arr, n):
		s = list(set(arr))
		max1 = -1
		for i in s:
			if i >= max1:
				max1 = i
		max2 = -1
		for i in s:
			if i >= max2 and i < max1:
				max2 = i
		return max2


class Solution:

	def print2largest(self, arr, n):
		max_num = arr[0]
		second_max = -1
		for i in arr:
			if i > max_num:
				second_max = max_num
				max_num = i
			elif i < max_num and i > second_max:
				second_max = i
		return second_max


class Solution:

	def print2largest(self, arr, n):
		Max = -1
		secMax = -1
		for i in arr:
			if i > Max:
				secMax = Max
				Max = i
			elif i > secMax and i < Max:
				secMax = i
		return secMax


class Solution:

	def print2largest(self, arr, n):
		first = arr[0]
		second = float('-inf')
		for x in arr:
			if x > first:
				first = x
		for x in arr:
			if x >= second and x != first:
				second = x
		if second == float('-inf'):
			return -1
		return second


class Solution:

	def print2largest(self, arr, n):
		if n != 1:
			largest = max(arr[0], arr[1])
			if arr[0] != arr[1]:
				secondlargest = min(arr[0], arr[1])
			else:
				secondlargest = 0
			flag = 0
			for i in range(2, n):
				if arr[i] > largest and arr[i] > secondlargest:
					secondlargest = largest
					largest = arr[i]
					flag = 1
				elif arr[i] < largest and arr[i] > secondlargest:
					secondlargest = arr[i]
					flag = 1
			if flag == 0:
				return -1
			return secondlargest
		else:
			return -1


class Solution:

	def print2largest(self, arr, n):
		x = list(set(arr))
		x.sort()
		try:
			return x[-2]
		except:
			return -1


class Solution:

	def print2largest(self, arr, n):
		maxi = arr[0]
		maxi2 = float('-inf')
		for i in range(1, len(arr)):
			if arr[i] <= maxi and arr[i] <= maxi2 or arr[i] == maxi or arr[i] == maxi2:
				continue
			elif arr[i] < maxi and arr[i] > maxi2:
				maxi2 = arr[i]
			else:
				maxi2 = maxi
				maxi = arr[i]
		if maxi2 == float('-inf'):
			return -1
		else:
			return maxi2


class Solution:

	def print2largest(self, arr, n):
		max_ = arr[0]
		s_max_ = -1
		for ele in arr:
			if max_ < ele:
				s_max_ = max_
				max_ = ele
			elif max_ > ele and ele > s_max_:
				s_max_ = ele
		return s_max_


class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			return -1
		if n == 2:
			return min(arr)
		first = -2454635434
		second = -2454635434
		for i in range(len(arr)):
			first = max(first, arr[i])
		for i in range(len(arr)):
			if arr[i] != first:
				second = max(second, arr[i])
		if second == -2454635434:
			return -1
		return second


import copy

class Solution:

	def print2largest(self, arr, n):
		m = max(arr)
		arr1 = arr.copy()
		for i in range(n):
			if arr1[i] != m:
				arr1[i] = m - arr1[i]
		idx = arr1.index(min(arr1))
		if arr[idx] == m:
			return -1
		return arr[idx]


class Solution:

	def print2largest(self, arr, n):
		maximum = 0
		maximum1 = -1
		position = []
		for i in range(n):
			if arr[i] > maximum:
				maximum = arr[i]
		for i in range(n):
			if arr[i] == maximum:
				position.append(i)
		for i in range(len(position)):
			arr.pop(position[i] - i)
		for i in range(len(arr)):
			if arr[i] > maximum1:
				maximum1 = arr[i]
		return maximum1


class Solution:

	def print2largest(self, arr, n):
		if n < 2:
			return -1
		largest = sec_largest = float('-inf')
		for i in range(0, n):
			if arr[i] > largest:
				sec_largest = largest
				largest = arr[i]
			elif arr[i] > sec_largest and arr[i] != largest:
				sec_largest = arr[i]
		if sec_largest == float('-inf'):
			return -1
		return sec_largest


class Solution:

	def print2largest(self, arr, n):
		largest = arr[0]
		second = -1
		for i in range(1, n):
			if arr[i] > largest:
				temp = largest
				second = temp
				largest = arr[i]
			elif arr[i] < largest and arr[i] > second:
				second = arr[i]
		return second


class Solution:

	def print2largest(self, arr, n):
		firstLargest = -1
		secondLargest = -1
		for num in arr:
			if num > firstLargest:
				secondLargest = firstLargest
				firstLargest = num
			elif num > secondLargest and num != firstLargest:
				secondLargest = num
		return secondLargest


class Solution:

	def print2largest(self, arr, n):
		unique = set(arr)
		unique_list = list(unique)
		unique_list.sort()
		if len(unique_list) == 1:
			return -1
		else:
			newnumber = int(len(unique_list))
			second = unique_list[newnumber - 2]
			return second


class Solution:

	def print2largest(self, arr, n):
		sortedArr = sorted(list(set(arr)), reverse=True)
		if len(sortedArr) <= 1:
			return -1
		return sortedArr[1]


class Solution:

	def print2largest(self, arr, n):
		first_largest = 0
		second_largest = 0
		for element in arr:
			if element > first_largest:
				second_largest = first_largest
				first_largest = element
			elif first_largest > element > second_largest:
				second_largest = element
		if first_largest == second_largest or second_largest == 0:
			return -1
		else:
			return second_largest


class Solution:

	def print2largest(self, arr, n):
		big_ele = -1
		sec_big_ele = -1
		for ele in arr:
			if ele > big_ele:
				big_ele = ele
		for ele in arr:
			if ele > sec_big_ele and ele < big_ele:
				sec_big_ele = ele
		return sec_big_ele


class Solution:

	def print2largest(self, arr, n):
		a = -1
		b = -1
		for i in range(n):
			a = max(arr[i], a)
		for i in range(n):
			if arr[i] != a:
				b = max(arr[i], b)
		return b


class Solution:

	def print2largest(self, arr, n):
		(l, sl) = (-1, -1)
		for i in arr:
			if l < i:
				sl = l
				l = i
			if sl < i and i != l:
				sl = i
		if sl == l:
			return -1
		return sl