Given an array of integers, your task is to find the smallest and second smallest element in the array. If smallest and second smallest do not exist, print -1.
Example 1:
Input :
5
2 4 3 5 6
Output :
2 3 
Explanation: 
2 and 3 are respectively the smallest 
and second smallest elements in the array.
Example 2:
Input :
6
1 2 1 3 6 7
Output :
1 2 
Explanation: 
1 and 2 are respectively the smallest 
and second smallest elements in the array.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minAnd2ndMin() which takes the array A[] and its size N as inputs and returns a vector containing the smallest and second smallest element if possible, else return {-1,-1}.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=10^{5}
1<=A[i]<=10^{5}

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

def minAnd2ndMin( a, n):
    #code here
    
    



Solutions:
def minAnd2ndMin(a, n):
	a = list(set(a))
	a.sort()
	l = a[:2]
	if len(l) == 2:
		return l
	else:
		return (-1, -1)


def minAnd2ndMin(a, n):
	mset = set(a)
	mlist = list(mset)
	mlist.sort()
	return [-1] if len(mlist) < 2 else [mlist[0], mlist[1]]


def minAnd2ndMin(a, n):
	b = sorted(set(a))
	if len(b) > 1:
		return (b[0], b[1])
	else:
		return (-1, -1)


def minAnd2ndMin(a, n):
	if n < 2 or a.count(a[0]) == n:
		return [-1, -1]
	else:
		a.sort()
		i = 1
		while a[0] == a[i]:
			i += 1
		return [a[0], a[i]]


def minAnd2ndMin(a, n):
	min1 = a[0]
	min2 = float('inf')
	for i in range(1, n):
		if a[i] < min1:
			min2 = min1
			min1 = a[i]
		elif a[i] < min2 and a[i] != min1:
			min2 = a[i]
	if n == 1 or min2 == float('inf'):
		return [-1, -1]
	else:
		return [min1, min2]


def minAnd2ndMin(a, n):
	se = set(a)
	li = list(se)
	li.sort()
	x = len(li)
	if x == 1:
		li2 = [-1]
		return li2
	else:
		return li


def minAnd2ndMin(a, n):
	a.sort()
	min1 = a[0]
	min2 = min1
	for i in a:
		if i > min2:
			min2 = i
			break
	if min1 == min2:
		return (-1, -1)
	else:
		return (min1, min2)


def minAnd2ndMin(a, n):
	m1 = -1
	m2 = -1
	for i in range(len(a)):
		if i == 0:
			m1 = a[i]
		if a[i] > m1 and m2 == -1:
			m2 = a[i]
		if a[i] > m1 and a[i] < m2:
			m2 = a[i]
		if a[i] < m1:
			m2 = m1
			m1 = a[i]
	if m2 == -1:
		return [-1, -1]
	return [m1, m2]


def minAnd2ndMin(a, n):
	a = list(set(a))
	a.sort()
	for i in a:
		if len(a) >= 2:
			return (a[0], a[1])
	else:
		return (-1, -1)


def minAnd2ndMin(a, n):
	if n == 1:
		return [-1, -1]
	elif n == 2:
		if a[0] == a[1]:
			return [-1, -1]
		else:
			return [min(a[0], a[1]), max(a[0], a[1])]
	(s1, s2, count) = (a[0], 1000000, 0)
	for i in range(1, n):
		if s1 < a[i] < s2:
			s2 = a[i]
			count += 1
		elif a[i] < s1:
			s2 = s1
			s1 = a[i]
			count += 1
	return [-1, -1] if count == 0 else [s1, s2]


def minAnd2ndMin(arr, n):
	arr.sort()
	number = arr[0]
	if len(arr) <= 1:
		return [-1]
	else:
		for i in range(1, n):
			if arr[i] == number:
				pass
			else:
				number = arr[i]
				break
		if number == arr[0]:
			return [-1]
		return (arr[0], number)


def minAnd2ndMin(a, n):
	a.sort()
	if n == 1:
		return [-1]
	res = [0, 0]
	for i in range(n - 1):
		if a[i] != a[i + 1]:
			res[0] = a[i]
			res[1] = a[i + 1]
			return res
	return [-1]


def minAnd2ndMin(a, n):
	a = list(set(a))
	a = [int(each) for each in a]
	if len(a) >= 2:
		z = min(a)
		a.remove(z)
		x = min(a)
		return [z, x]
	else:
		return [-1]


def minAnd2ndMin(a, n):
	t = list(set(a))
	t.sort()
	if len(t) == 0:
		return (-1, -1)
	elif len(t) == 1:
		return [-1]
	else:
		return (t[0], t[1])


def minAnd2ndMin(a, n):
	L = [0, 0]
	a = set(a)
	if len(a) > 1:
		L[0] = min(a)
		a.remove(L[0])
		L[1] = min(a)
		return L
	else:
		return [-1, -1]


def minAnd2ndMin(arr, n):
	small = arr[0]
	secondsmall = 1000000000.0
	f = False
	for i in range(1, n):
		if arr[i] < small:
			secondsmall = small
			small = arr[i]
			f = True
		elif arr[i] > small and arr[i] < secondsmall:
			secondsmall = arr[i]
			f = True
	if f:
		return (small, secondsmall)
	else:
		return (-1, -1)


def minAnd2ndMin(arr, n):
	d = {}
	for i in range(n):
		if arr[i] not in d:
			d[arr[i]] = 1
		else:
			d[arr[i]] += 1
	if len(d) == 1:
		return (-1, -1)
	first_min = min(d)
	second_min = 1000000000.0
	for j in d:
		if j > first_min:
			second_min = min(second_min, j)
	return (first_min, second_min)


def minAnd2ndMin(a, n):
	first_min = min(a)
	first_to_second_diff = float('inf')
	second_min = -1
	for num in a:
		if num > first_min and num - first_min < first_to_second_diff:
			first_to_second_diff = num - first_min
			second_min = num
	if len(set(a)) == 1:
		return [-1, -1]
	return [first_min, second_min]


def minAnd2ndMin(a, n):
	first_min = min(a)
	if len(set(a)) == 1:
		first_min = -1
	second_min = -1
	if len(set(a)) > 1:
		second_min = sorted(list(set(a)))[1]
	return [first_min, second_min]


def minAnd2ndMin(a, n):
	res = []
	if n < 2:
		res.append(-1)
	smallest = float('inf')
	ssmallest = float('inf')
	temp = ssmallest
	for i in range(n):
		if a[i] < smallest:
			ssmallest = smallest
			smallest = a[i]
		if a[i] < ssmallest and a[i] != smallest:
			ssmallest = a[i]
	if smallest == ssmallest or ssmallest == temp:
		res.append(-1)
	res.append(smallest)
	res.append(ssmallest)
	return res


def minAnd2ndMin(a, n):
	if len(a) <= 1:
		return (-1, -1)
	a.sort()
	for i in a:
		if i > a[0]:
			return (a[0], i)
	return [-1]


def minAnd2ndMin(a, n):
	new_a = list(set(a))
	if len(new_a) < 2:
		return (-1, -1)
	else:
		small = new_a.pop(new_a.index(min(new_a)))
		second_small = min(new_a)
		return (small, second_small)


def minAnd2ndMin(a, n):
	b = set(a)
	b = list(b)
	b.sort()
	if len(b) < 2:
		return (-1, -1)
	return (b[0], b[1])


def minAnd2ndMin(a, n):
	min_num = min(a)
	min_diff = float('inf')
	second_min = -1
	for num in a:
		if num > min_num and num - min_num < min_diff:
			min_diff = num - min_num
			second_min = num
	if len(a) == 1:
		return [-1, -1]
	if len(set(a)) == 1:
		return [-1]
	return [min_num, second_min]


def minAnd2ndMin(a, n):
	first = max(a)
	second = max(a)
	for num in a:
		if num < first:
			second = first
			first = num
		elif num < second and num != first:
			second = num
	if len(a) == 0:
		return [-1, -1]
	if len(set(a)) == 1:
		return [-1]
	return [first, second]


import sys

def minAnd2ndMin(a, n):
	secondSmallest = firstSmallest = 9999999
	for i in a:
		if i < firstSmallest and i < secondSmallest:
			secondSmallest = firstSmallest
			firstSmallest = i
		if i > firstSmallest and i < secondSmallest:
			secondSmallest = i
	if firstSmallest == 9999999 or secondSmallest == 9999999:
		return [-1]
	return [firstSmallest, secondSmallest]


def minAnd2ndMin(a, n):
	small = 100000000
	ssmall = 100000000
	for i in range(n):
		if a[i] < small:
			if small < ssmall:
				ssmall = small
			small = a[i]
		elif a[i] > small and a[i] < ssmall:
			ssmall = a[i]
	if ssmall == 100000000:
		return (-1, -1)
	return (small, ssmall)


def minAnd2ndMin(a, n):
	a = list(set(a))
	a.sort()
	if a and len(a) > 1:
		return (a[0], a[1])
	return (-1, -1)


def minAnd2ndMin(a, n):
	ans = []
	if n == 1 or (n == 2 and a[0] == a[1]):
		ans.append(-1)
		ans.append(-1)
	elif a.count(min(a)) == n:
		ans.append(-1)
		ans.append(-1)
	else:
		a.sort()
		ans.append(min(a))
		ans.append(a[a.count(min(a))])
	return ans


def minAnd2ndMin(a, n):
	ax = a.count(a[0])
	if ax == n:
		return (-1, -1)
	elif n == 0:
		return (-1, -1)
	else:
		a = list(set(a))
		a.sort()
		return (a[0], a[1])


def minAnd2ndMin(a, n):
	a.sort()
	b = []
	if n < 2:
		return [-1, -1]
	elif a[0] != a[1]:
		b.append(a[0])
		b.append(a[1])
		return b
	elif len(a) >= 2:
		i = 1
		while a[0] == a[i]:
			if i == n - 1:
				return [-1, -1]
			i = i + 1
		b.append(a[0])
		b.append(a[i])
		return b


def minAnd2ndMin(a, n):
	p = sorted(a)
	list1 = [p[0]]
	for i in p:
		if i != p[0]:
			list1.append(i)
			break
	if len(list1) == 1:
		return [-1]
	return list1


def minAnd2ndMin(a, n):
	ans = []
	x = list(set(a))
	x.sort()
	if len(x) >= 2:
		ans.append(x[0])
		ans.append(x[1])
	else:
		ans.append(-1)
	return ans


def minAnd2ndMin(a, n):
	if len(set(a)) >= 2:
		a = sorted(set(a))
		return (a[0], a[1])
	return [-1]


def minAnd2ndMin(a, n):
	a.sort()
	p = a[0]
	s = 0
	for i in range(len(a) - 1):
		if a[i] == a[0]:
			s = s + 1
	for i in range(s):
		a.remove(p)
	if p != a[0]:
		return (p, a[0])
	else:
		return (-1, -1)


def minAnd2ndMin(a, n):
	first_smallest = second_smallest = float('inf')
	for i in a:
		if i < first_smallest:
			second_smallest = first_smallest
			first_smallest = i
		elif i < second_smallest and i != first_smallest:
			second_smallest = i
	if n == 1 or second_smallest == float('inf'):
		return (-1, -1)
	else:
		return (first_smallest, second_smallest)


def minAnd2ndMin(a, n):
	arr = list(set(a))
	arr.sort()
	if len(arr) > 1:
		return (arr[0], arr[1])
	else:
		return (-1, -1)


def minAnd2ndMin(a, n):
	a.sort()
	m1 = a[0]
	m2 = False
	for i in range(1, n):
		if a[i] != m1:
			m2 = True
			m = a[i]
			break
	if m2:
		return [m1, m]
	else:
		return [-1, -1]


def minAnd2ndMin(arr, n):
	smallest = float('inf')
	second_smallest = float('inf')
	for num in arr:
		if num < smallest:
			second_smallest = smallest
			smallest = num
		elif num < second_smallest and num != smallest:
			second_smallest = num
	if second_smallest == float('inf'):
		return (-1, -1)
	else:
		return (smallest, second_smallest)


def minAnd2ndMin(a, n):
	a1 = list(set(a))
	a1.sort()
	if len(a1) >= 2:
		return a1[:2]
	return (-1, -1)


def minAnd2ndMin(arr, n):
	a = []
	if n < 2:
		a.append(-1)
		return a
	arr.sort()
	i = 0
	c = 0
	j = 1
	while arr[i] == arr[j]:
		i = i + 1
		j = j + 1
		if j == n:
			c = 1
			break
	if c != 1:
		a.append(arr[i])
		a.append(arr[j])
		return a
	else:
		a.append(-1)
		return a


def minAnd2ndMin(a, n):
	t = sorted(set(a))
	if len(t) == 0:
		return (-1, -1)
	elif len(t) == 1:
		return [-1]
	else:
		return (t[0], t[1])


def minAnd2ndMin(a, n):
	mini = min(a)
	b = [-1, -1]
	mini2 = -1
	diff = 999999
	for i in a:
		if i > mini:
			if abs(i - mini) < diff:
				diff = abs(i - mini)
				mini2 = i
	if mini2 == -1:
		return b
	b[0] = mini
	b[1] = mini2
	return b


def minAnd2ndMin(a, n):
	if n == 0:
		return [-1]
	s = set(a)
	if len(s) == 1:
		return [-1]
	a = sorted(s)
	b = []
	b.append(a[0])
	b.append(a[1])
	return b


def minAnd2ndMin(a, n):
	import numpy as np
	min1 = 100001
	min2 = 100001
	for e in a:
		if e < min1:
			min2 = min1
			min1 = e
		elif e == min1:
			continue
		elif e < min2:
			min2 = e
	if min2 == 100001:
		return np.array([-1])
	list = [min1, min2]
	arr = np.array(list)
	return arr


def minAnd2ndMin(a, n):
	my_set = set(a)
	a = list(my_set)
	a.sort()
	if len(a) >= 2:
		return (a[0], a[1])
	else:
		return (-1, -1)


def minAnd2ndMin(a, n):
	if n == 1:
		return (-1, -1)
	b = a.copy()
	lst = []
	(cnt, ans) = (0, 0)
	for i in range(2):
		ans = min(b)
		lst.append(ans)
		for i in b:
			if ans == i:
				cnt += 1
		if cnt == len(a):
			return (-1, -1)
		for i in range(cnt):
			b.remove(ans)
		(ans, cnt) = (0, 0)
	return lst


def minAnd2ndMin(a, n):
	ans = sorted(set(a))
	if len(ans) >= 2:
		return ans[:2]
	return [-1, -1]


def minAnd2ndMin(a, n):
	l = []
	t = [-1, -1]
	s = list(set(a))
	if len(s) == 1:
		return t
	s.sort()
	l.append(s[0])
	l.append(s[1])
	return l


def minAnd2ndMin(a, n):
	try:
		mylist = []
		a = sorted(set(a))
		mylist.append(a[0])
		mylist.append(a[1])
	except:
		mylist = [-1, -1]
		return mylist
	return mylist


def minAnd2ndMin(a, n):
	if n >= 2:
		first = min(a)
		while first in a:
			a.remove(first)
		if len(a) >= 1:
			second = min(a)
			return [first, second]
		return [-1]
	return [-1]