Given an array of distinct elements. Find the third largest element in it. 
Suppose you have A[] = {1, 2, 3, 4, 5, 6, 7}, its output will be 5 because it is the 3 largest element in the array A.
Example 1:
Input:
N = 5
A[] = {2,4,1,3,5}
Output: 3
Example 2:
Input:
N = 2
A[] = {10,2}
Output: -1
Your Task:
Complete the function thirdLargest() which takes the array a[] and the size of the array, n, as input parameters and returns the third largest element in the array. It return -1 if there are less than 3 elements in the given array.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: class Solution:
    def thirdLargest(self,a, n):
        # code here

Solutions:
class Solution:

	def thirdLargest(self, a, n):
		if n >= 3:
			a.sort()
			return a[-3]
		else:
			return -1


class Solution:

	def thirdLargest(self, a, n):
		if len(a) <= 3:
			return -1
		else:
			a.sort(reverse=True)
			return a[2]


class Solution:

	def thirdLargest(self, a, n):
		t = sorted(a)
		return t[-3]


class Solution:

	def thirdLargest(self, a, n):
		if len(a) < 3:
			return -1
		else:
			a = sorted(a)
			return a[-3]


class Solution:

	def thirdLargest(self, a, n):
		arr.sort()
		return arr[-3]


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		for i in range(n):
			if n < 3:
				return -1
			else:
				return a[-3]


class Solution:

	def thirdLargest(self, a, n):
		a.sort(reverse=True)
		count = 1
		previous = a[0]
		for i in range(len(a)):
			if a[i] != previous:
				count = count + 1
				previous = a[i]
			if count == 3:
				return a[i]
		return a[0]


class Solution:

	def thirdLargest(self, a, n):
		big1 = -1
		big2 = -1
		big3 = -1
		for i in a:
			if i > big3:
				big3 = i
			if big3 > big2:
				(big2, big3) = (big3, big2)
			if big2 > big1:
				(big1, big2) = (big2, big1)
		return big3


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		a.remove(max(a))
		a.remove(max(a))
		if a:
			return max(a)


class Solution:

	def thirdLargest(self, a, n):
		a = sorted(a, reverse=True)
		x = a[2]
		return x


class Solution:

	def thirdLargest(self, a, n):
		if n < 3:
			return -1
		one = float('-inf')
		two = float('-inf')
		three = float('-inf')
		for elem in a:
			if elem > one:
				if one != float('-inf'):
					temp = one
					one = elem
					temp1 = two
					two = temp
					three = temp1
				else:
					one = elem
			elif elem > two:
				if two != float('-inf'):
					temp = two
					two = elem
					three = temp
				else:
					two = elem
			else:
				three = max(three, elem)
		return three


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		b = a[::-1]
		for i in b:
			return b[2]
		return -1


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		if len(a) <= 2:
			return -1
		else:
			a.pop()
			a.pop()
			return max(a)


class Solution:

	def thirdLargest(self, a, n):
		if len(a) < 3:
			return 0
		a.sort()
		return a[len(a) - 3]


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		t = arr[len(arr) - 3]
		return t


class Solution:

	def thirdLargest(self, a, n):
		arr = a
		if n < 3:
			return -1
		first = arr[0]
		second = arr[1]
		third = arr[2]
		for i in range(1, n):
			if arr[i] > first:
				third = second
				second = first
				first = arr[i]
			elif arr[i] > second:
				third = second
				second = arr[i]
			elif arr[i] > third:
				third = arr[i]
		return third


class Solution:

	def thirdLargest(self, a, n):
		if n < 3:
			return -1
		(max1, max2, max3) = (a[0], -1, -1)
		for i in a[1:]:
			if i > max1:
				(max3, max2, max1) = (max2, max1, i)
			elif i > max2:
				(max3, max2) = (max2, i)
			elif i > max3:
				max3 = i
		return max3


import random

class Solution:

	def thirdLargest(self, a, n):
		if len(a) < 3:
			return -1
		first = a[0]
		for i in range(1, n):
			if a[i] > first:
				first = a[i]
		second = -1000
		for i in range(0, n):
			if a[i] > second and a[i] < first:
				second = a[i]
		third = -1000
		for i in range(0, n):
			if a[i] > third and a[i] < second:
				third = a[i]
		return third


class Solution:

	def thirdLargest(self, a, n):
		(i, j, k) = (-1, 0, 0)
		for t in range(n):
			if a[t] > k:
				i = max(i, j)
				j = max(i, k)
				k = a[t]
			elif a[t] > j:
				i = max(i, j)
				j = a[t]
			elif a[t] > i:
				i = a[t]
			else:
				continue
		return i


class Solution:

	def thirdLargest(self, a, n):
		high = -float('inf')
		sec = -float('inf')
		third = -float('inf')
		for i in a:
			if i > high:
				sec = high
				high = i
			if i > sec and i < high:
				sec = i
		for i in a:
			if i > third and i < sec:
				third = i
		return third


class Solution:

	def thirdLargest(self, array, n):
		if n < 3:
			return -1
		first = second = third = 0
		first = array[0]
		for i in range(1, n):
			data = array[i]
			if first < data:
				third = second
				second = first
				first = data
			elif second < data:
				third = second
				second = data
			elif third < data:
				third = data
		return third


class Solution:

	def thirdLargest(self, a, n):
		b = sorted(a)
		c = b[::-1]
		return c[2]


class Solution:

	def thirdLargest(self, a, n):
		if n < 3:
			return -1
		first = second = third = float('-inf')
		for num in a:
			if num > first:
				third = second
				second = first
				first = num
			elif second < num < first:
				third = second
				second = num
			elif third < num < second:
				third = num
		if third == float('-inf'):
			return -1
		else:
			return third


class Solution:

	def thirdLargest(self, a, n):
		b = a[:]
		b.sort()
		b.reverse()
		if n >= 3:
			return b[2]
		else:
			return -1


class Solution:

	def thirdLargest(self, a, n):
		if n < 3:
			return -1
		max1 = max2 = max3 = float('-inf')
		for i in range(n):
			if a[i] > max1:
				max3 = max2
				max2 = max1
				max1 = a[i]
			elif a[i] > max2 and a[i] <= max1:
				max3 = max2
				max2 = a[i]
			elif a[i] > max3 and a[i] <= max2:
				max3 = a[i]
		return max3 if max3 != float('-inf') else -1


class Solution:

	def thirdLargest(self, a, n):
		if len(a) > 2:
			res = a.sort(reverse=True)
			return a[2]
		elif len(a) <= 2:
			return -1


class Solution:

	def thirdLargest(self, a, n):
		if n < 3:
			return -1
		first = -1
		second = -1
		third = -1
		for i in a:
			if i > first:
				third = second
				second = first
				first = i
			elif i > second:
				third = second
				second = i
			elif i > third:
				third = i
		return third


class Solution:

	def thirdLargest(self, a, n):
		a = list(set(a))
		if len(a) < 3:
			return -1
		arr.sort(reverse=True)
		return arr[2]


class Solution:

	def thirdLargest(self, a, n):
		if n < 3:
			return -1
		a = sorted(a, reverse=True)
		return a[2]


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		b = a.pop()
		c = a.pop()
		return a.pop()


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		a.reverse()
		return a[2]


class Solution:

	def thirdLargest(self, a, n):
		lst = list(a)
		for i in range(3):
			m = max(lst)
			lst.remove(m)
		return m


class Solution:

	def thirdLargest(self, a, n):
		t = list(set(a))
		t.sort()
		if len(t) <= 2:
			return -1
		else:
			return t[-3]


import sys

class Solution:

	def thirdLargest(self, a, n):
		if len(a) < 3:
			return -1
		third = first = second = -sys.maxsize
		for i in a:
			if i > first:
				third = second
				second = first
				first = i
			elif i > second:
				third = second
				second = i
			elif i > third:
				third = i
		return third


class Solution:

	def thirdLargest(self, a, n):
		x = 0
		maxi = float('-inf')
		maxi2 = maxi
		maxi3 = maxi
		for i in a:
			if i > maxi:
				maxi3 = maxi2
				maxi2 = maxi
				maxi = i
			elif i > maxi2:
				maxi3 = maxi2
				maxi2 = i
			elif i > maxi3:
				maxi3 = i
		return maxi3


class Solution:

	def thirdLargest(self, a, n):
		x = 0
		maxi = 0
		for i in a:
			if i > maxi:
				maxi = i
		maxi2 = 0
		for i in a:
			if i > maxi2 and i < maxi:
				maxi2 = i
		maxi3 = 0
		for i in a:
			if i > maxi3 and i < maxi2:
				maxi3 = i
		return maxi3


class Solution:

	def thirdLargest(self, a, n):
		if len(a) < 3:
			return -1
		b = a.remove(max(a))
		c = a.remove(max(a))
		return max(a)


class Solution:

	def thirdLargest(self, a, n):
		b = sorted(a)
		return b[-3]


class Solution:

	def thirdLargest(self, a, n):
		ul = list(set(a))
		ul.sort()
		return ul[-3]


class Solution:

	def thirdLargest(self, a, n):
		a.sort()
		r = a[::-1]
		return r[2]


class Solution:

	def thirdLargest(self, a, n):
		del a[a.index(max(a))]
		del a[a.index(max(a))]
		return max(a)