Given an array of N distinct elements, the task is to find all elements in array except two greatest elements in sorted order.
Example 1:
Input : 
a[] = {2, 8, 7, 1, 5}
Output :
1 2 5 
Explanation :
The output three elements have two or
more greater elements.   
Example 2:
Input :
a[] = {7, -2, 3, 4, 9, -1}
Output :
-2 -1 3 4
Your Task:  
You don't need to read input or print anything. Your task is to complete the function findElements() which takes the array A[] and its size N as inputs and return the vector sorted values denoting the elements in array which have at-least two greater elements than themselves.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
3 ≤ N ≤ 10^{5}
-10^{6} ≤ A_{i} ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3



class Solution:

    def findElements(self,a, n):

        # Your code goes here

    



Solutions:
class Solution:

	def findElements(self, a, n):
		a.sort()
		return a[:n - 2]


class Solution:

	def findElements(self, a, n):
		a = list(set(a))
		a.sort()
		return a[0:n - 2]


class Solution:

	def findElements(self, a, n):
		return sorted(a)[:-2]


class Solution:

	def findElements(self, a, n):
		a.sort()
		l = list(a)
		return a[:-2]


class Solution:

	def mergeSort(self, arr, l, h):
		if l == h:
			return
		mid = (l + h) // 2
		self.mergeSort(arr, l, mid)
		self.mergeSort(arr, mid + 1, h)
		i = l
		j = mid + 1
		newarr = list()
		while i <= mid and j <= h:
			if arr[i] < arr[j]:
				newarr.append(arr[i])
				i += 1
			else:
				newarr.append(arr[j])
				j += 1
		while i <= mid:
			newarr.append(arr[i])
			i += 1
		while j <= h:
			newarr.append(arr[j])
			j += 1
		arr[l:h + 1] = newarr

	def findElements(self, a, n):
		self.mergeSort(a, 0, n - 1)
		return a[0:n - 2]


class Solution:

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


class Solution:

	def findElements(self, a, n):
		a.sort()
		first_largest = a[-1]
		second_largest = a[-2]
		a = a[:-2]
		return a


class Solution:

	def findElements(self, arr, n):
		if arr[1] < arr[0]:
			(largest, second) = (arr[0], arr[1])
		else:
			(largest, second) = (arr[1], arr[0])
		for a in arr[2:]:
			if a > largest:
				second = largest
				largest = a
			elif a > second:
				second = a
		arr.remove(largest)
		arr.remove(second)
		return sorted(arr)


class Solution:

	def findElements(self, a, n):
		a.sort()
		a.pop()
		a.pop()
		return a


class Solution:

	def findElements(self, arr, n):
		sort_arr = sorted(arr)
		return sort_arr[:-2]


class Solution:

	def findElements(self, a, n):
		a.sort()
		res = []
		for i in range(n - 2):
			res.append(a[i])
		return res


class Solution:

	def findElements(self, a, n):
		new_arr = []
		n = len(a)
		a.sort()
		for i in range(n - 2):
			new_arr.append(a[i])
		return new_arr


class Solution:

	def findElements(self, a, n):
		a = list(set(a))
		a.sort()
		op = []
		for i in range(n - 2):
			op.append(a[i])
		return op


class Solution:

	def findElements(self, a, n):
		a.sort()
		b = a[-1]
		c = a[-2]
		a.remove(b)
		a.remove(c)
		return a


class Solution:

	def findElements(self, a, n):
		b = a
		b.sort()
		b.pop(-1)
		b.pop(-1)
		return b


class Solution:

	def findElements(self, a, n):
		a.sort()
		a.pop(n - 1)
		a.pop(n - 2)
		return a


class Solution:

	def findElements(self, a, n):
		result = []
		a.sort()
		a.remove(max(a))
		a.remove(max(a))
		return a


class Solution:

	def findElements(self, a, n):
		m = a.index(max(a))
		del a[m]
		n = a.index(max(a))
		del a[n]
		return sorted(a)


class Solution:

	def findElements(self, a, n):
		sroteda = sorted(list(a))
		final_s = sroteda[:-2]
		return final_s


class Solution:

	def findElements(self, a, n):
		lst = []
		b = sorted(a)
		for i in range(0, n - 2):
			lst.append(b[i])
		return lst


def merge_elements(a, l, mid, r):
	n1 = mid - l + 1
	n2 = r - mid
	left = [0] * n1
	right = [0] * n2
	for i in range(0, n1):
		left[i] = a[l + i]
	for j in range(0, n2):
		right[j] = a[mid + 1 + j]
	i = 0
	j = 0
	k = l
	while i < n1 and j < n2:
		if left[i] <= right[j]:
			a[k] = left[i]
			i += 1
		else:
			a[k] = right[j]
			j += 1
		k += 1
	while i < n1:
		a[k] = left[i]
		i += 1
		k += 1
	while j < n2:
		a[k] = right[j]
		j += 1
		k += 1

def sort_element(a, l, r):
	if l < r:
		mid = (l + r) // 2
		sort_element(a, l, mid)
		sort_element(a, mid + 1, r)
		merge_elements(a, l, mid, r)

class Solution:

	def findElements(self, a, n):
		sort_element(a, 0, n - 1)
		return a[:-2]


class Solution:

	def findElements(self, a, n):
		a.sort()
		arr = a[0:-2]
		return arr


class Solution:

	def findElements(self, a, n):
		l = []
		a = sorted(a)
		for i in range(n - 2):
			l.append(a[i])
		return l


class Solution:

	def findElements(self, a, n):
		a.sort()
		b = []
		for i in range(0, len(a) - 2):
			b.append(a[i])
		return b


class Solution:

	def findElements(self, a, n):
		a_sort = sorted(a)
		return a_sort[0:n - 2]


class Solution:

	def findElements(self, a, n):
		s = set(a)
		a = list(s)
		a.sort()
		t = a[::-1]
		t.pop(0)
		t.pop(0)
		return t[::-1]


class Solution:

	def findElements(self, a, n):
		if len(a) <= 2:
			return a
		a = sorted(a)
		return a[:-2]


class Solution:

	def findElements(self, arr, n):
		arr.sort()
		arr.pop()
		arr.pop()
		return arr


class Solution:

	def findElements(self, arr, n):
		arr.sort()
		l = []
		if n > 2:
			for i in range(0, n - 2, 1):
				l.append(arr[i])
		arr.clear()
		return l
		return a


class Solution:

	def findElements(self, a, n):
		a.sort()
		my_list = a[0:n - 2]
		return my_list


class Solution:

	def findElements(self, a, n):
		a.sort()
		b = a[0:n - 2]
		return b


class Solution:

	def findElements(self, a, n):
		max1 = max2 = float('-inf')
		for i in range(n):
			if a[i] > max1:
				max2 = max1
				max1 = a[i]
			elif a[i] > max2:
				max2 = a[i]
		res = []
		for i in range(n):
			if a[i] < max2:
				res.append(a[i])
		res.sort()
		return res


class Solution:

	def findElements(self, a, n):
		if n <= 2:
			return []
		max1 = max2 = float('-inf')
		for i in range(n):
			if a[i] > max1:
				max2 = max1
				max1 = a[i]
			elif a[i] > max2:
				max2 = a[i]
		result = []
		for i in range(n):
			if a[i] != max1 and a[i] != max2:
				result.append(a[i])
		result.sort()
		return result


class Solution:

	def findElements(self, a, n):
		a.sort(reverse=False)
		l = len(a) - 2
		return a[0:l]


class Solution:

	def findElements(self, a, n):
		a.sort()
		arr = list(a)
		return arr[:-2]


class Solution:

	def findElements(self, a, n):
		s = sorted(a, reverse=True)
		s.remove(max(s))
		s.remove(max(s))
		p = sorted(s)
		return p


class Solution:

	def findElements(self, a, n):
		temp = 0
		a.sort()
		return a[0:n - 2]