Given two positive numbers L and R (L < R), find out the smallest twins in the range [L , R].Two numbers are twins if they are prime numbers and difference between them is 2.
Example 1:
Input: L = 0, R = 5
Output: [3, 5]
Explanation: Difference between 3 and 5 is 2
and also these two are smallest prime in range.
So the answer is 3, 5. 
Example 2:
Input: L = 5, R = 10
Output: [5, 7]
Explanation: Difference between 5 and 7 is 2
and also these two are smallest prime in range.
So the answer is 5, 7. 
Your Task:  
You dont need to read input or print anything. Complete the function primeTwins() which takes L and R as input parameter and returns a pair if exist else returns -1.
Expected Time Complexity: O(nloglogn)
Expected Auxiliary Space: O(n)
Constraints:
0<= L <=500
2<= R <=1000

Difficulty: EASY
Tags: Number theory

Mathematics
Expected Time Complexity: O(nloglogn)
Expected Auxiliary Space: O(n)

Starter code:
#User function Template for python3

class Solution:
	def primeTwins(self, L, R):
		# code here


Solutions:
class Solution:

	def isprime(self, N):
		if N == 0:
			return False
		if N == 1:
			return False
		for i in range(2, int(N ** 0.5) + 1, 1):
			if N % i == 0:
				return False
		return True

	def primeTwins(self, L, R):
		l = []
		for i in range(L, R + 1, 1):
			if self.isprime(i) and self.isprime(i + 2):
				l.append(i)
				l.append(i + 2)
				break
		if len(l) == 0:
			l.append(-1)
		return l


class Solution:

	def primeTwins(self, L, R):
		n = R
		sieve = [True] * (n + 1)
		prime = []
		sieve[0] = sieve[1] = False
		for i in range(2, n + 1):
			if sieve[i]:
				for j in range(i * i, n + 1, i):
					sieve[j] = False
				prime.append(i)
		for i in range(len(prime) - 1):
			if prime[i] < L:
				continue
			if prime[i + 1] - prime[i] == 2:
				return [prime[i], prime[i + 1]]
		return [-1]


class Solution:

	def primeTwins(self, L, R):
		l = [1] * (R + 1)
		l[0] = 0
		l[1] = 0
		for i in range(2, R + 1):
			if l[i] == 1:
				for j in range(2 * i, R + 1, i):
					l[j] = 0
		ans = []
		gg = False
		for i in range(L, R - 1):
			if l[i] and l[i + 2]:
				ans.append(i)
				ans.append(i + 2)
				gg = True
				break
		if gg:
			return ans
		else:
			ans.append(-1)
			return ans


class Solution:

	def primeTwins(self, L, R):

		def prime(n):
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if L < 2:
			L = 2
		if R < 2:
			R = 2
		ind = 0
		l = [0, 0]
		for i in range(L, R + 1):
			if prime(i):
				l[ind] = i
				ind += 1
			if len(l) == 2 and l[1] != 0:
				x = abs(l[0] - l[1])
				if x == 2:
					return l
				l[0] = l[1]
				l[1] = 0
				ind = 1
		return [-1]


class Solution:

	def primeTwins(self, L, R):

		def prime(n):
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if L < 2:
			L = 2
		if R < 2:
			R = 2
		l = []
		for i in range(L, R + 1):
			if prime(i):
				l.append(i)
			if len(l) == 2:
				x = abs(l[0] - l[1])
				if x == 2:
					return l
				else:
					l.pop(0)
		return [-1]


class Solution:

	def primeTwins(self, L, n):
		N = n
		arr = [True] * (N + 1)
		arr[0] = False
		arr[1] = False
		for i in range(2, int(n ** 0.5) + 1):
			if arr[i]:
				for j in range(i * i, N + 1, i):
					arr[j] = False
		ret = list()
		for i in range(L, n - 1):
			if arr[i] and arr[i + 2]:
				return [i, i + 2]
		return [-1]
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(L, R) = input().split()
		L = int(L)
		R = int(R)
		ob = Solution()
		ans = ob.primeTwins(L, R)
		for i in range(len(ans)):
			print(ans[i], end=' ')
		print()


import math

class Solution:

	def isprime(self, n):
		if n <= 1:
			return False
		i = 2
		while i <= int(math.sqrt(n)):
			if n % i == 0:
				return False
			i += 1
		return True

	def primeTwins(self, L, R):
		arr = [0] * 2
		for i in range(L, R + 1):
			if self.isprime(i) and self.isprime(i + 2):
				arr[0] = i
				arr[1] = i + 2
				break
		if arr[0] == 0 and arr[1] == 0:
			arr1 = [0] * 1
			arr1[0] = -1
			return arr1
		return arr


class Solution:

	def primeTwins(self, L, R):
		p = 2
		l = []
		prime = [True for i in range(R + 1)]
		while p * p <= R:
			if prime[p]:
				for i in range(p * p, R + 1, p):
					prime[i] = False
			p = p + 1
		for i in range(L, R - 2 + 1):
			if prime[i] and prime[i + 2] and (i != 0) and (i != 1):
				l.append(i)
				l.append(i + 2)
				break
		if l:
			return l
		else:
			return [-1]


import math

class Solution:

	def primeTwins(self, L, R):
		l = 1001
		siv = [0] * l
		siv[0] = 1
		siv[1] = 1
		for i in range(2, int(math.sqrt(l)) + 1):
			if siv[i] == 0:
				for j in range(2 * i, l, i):
					siv[j] = 1
		lis = []
		for i in range(L, R + 1):
			if siv[i] == 0 and siv[i + 2] == 0 and (i + 2 <= R):
				lis.append(i)
				lis.append(i + 2)
				break
		if len(lis) == 0:
			lis.append(-1)
		return lis


class Solution:

	def primeTwins(self, L, R):
		from math import sqrt
		N = R
		k = []
		if N == 1 or N == 2:
			return [-1]
		l = [1] * (N + 1)
		l[0] = 0
		l[1] = 0
		k = []
		for i in range(2, int(sqrt(N)) + 1):
			if l[i] == 1:
				for j in range(i * i, N + 1, i):
					l[j] = 0
		for i in range(L, N - 1):
			if l[i] == 1 and l[i + 2] == 1:
				k.append(i)
				k.append(i + 2)
				break
		if len(k) == 0:
			return [-1]
		return k


class Solution:

	def primeTwins(self, L, R):
		l = []
		sieve = [1] * (R + 1)
		sieve[1] = sieve[0] = 0
		for i in range(2, R + 1):
			if sieve[i] == 1:
				for j in range(i * i, R + 1, i):
					sieve[j] = 0
		for i in range(L, R + 1):
			if sieve[i] == 1 and i + 2 <= R and (sieve[i + 2] == 1):
				l.append(i)
				l.append(i + 2)
				break
		if len(l) == 0:
			return [-1]
		return l


class Solution:

	def primeTwins(self, L, R):

		def seive(i):
			l = [1] * (i + 1)
			l[0] = 0
			l[1] = 0
			p = 2
			while p * p <= i:
				if l[p] == 1:
					for j in range(p * p, i + 1, p):
						l[j] = 0
				p += 1
			return l
		i = R
		res = seive(i)
		z = []
		m = []
		res = seive(i)
		for i in range(L, R + 1):
			if res[i] == 1:
				z.append(i)
		p = 1
		c = 0
		while p < len(z):
			if z[p] - z[p - 1] == 2:
				c += 1
				return [z[p - 1], z[p]]
			p += 1
		if c == 0:
			return [-1]


from math import sqrt

class Solution:

	def prime(self, n):
		if n == 0 or n == 1:
			return False
		if n == 2 or n == 3:
			return True
		if n % 2 == 0 or n % 3 == 0:
			return False
		for i in range(5, int(sqrt(n)) + 1, 6):
			if n % i == 0 or n % (i + 2) == 0:
				return False
		return True

	def primeTwins(self, l, r):
		c = []
		for i in range(l, r + 1):
			if self.prime(i):
				c.append(i)
		ni = len(c)
		niva = 2
		va = []
		for i in range(ni - 1):
			if niva == c[i + 1] - c[i]:
				va.append(c[i])
				va.append(c[i + 1])
				return va
		va = [-1]
		return va


class Solution:

	def primeTwins(self, L, R):

		def isprime(n):
			if n < 2:
				return False
			i = 2
			while i <= int(n ** 0.5):
				if n % i == 0:
					return False
				i = i + 1
			return True
		l = []
		while L <= R:
			if isprime(L):
				l.append(L)
			L = L + 1
		m = []
		if len(l) < 2:
			m.append(-1)
			return m
		if len(l) == 2:
			if l[1] - l[0] == 2:
				return l
			else:
				m.append(-1)
				return m
		i = 0
		while i <= len(l) - 1:
			if l[i + 1] - l[i] == 2:
				m.append(l[i])
				m.append(l[i + 1])
				return m
			i = i + 1
		return


class Solution:

	def primeTwins(self, L, R):

		def isprime(num):
			if num <= 1:
				return False
			if num == 2:
				return True
			for i in range(2, int(num ** 0.5) + 1):
				if num % i == 0:
					return False
			return True
		l = []
		while L <= R:
			if isprime(L):
				l.append(L)
			if len(l) == 2 and l[1] - l[0] == 2:
				return l
			if len(l) == 2 and l[1] - l[0] != 2:
				l.remove(l[0])
			L += 1
		l = []
		l.append(-1)
		return l


class Solution:

	def primeTwins(self, L, R):
		import math as m

		def primes(n):
			if n == 0 or n == 1:
				return False
			k = int(m.sqrt(n))
			for i in range(2, k + 1):
				if n % i == 0:
					return False
			return True
		l = []
		m1 = []
		for i in range(L, R + 1):
			if primes(i):
				l.append(i)
		for i in range(1, len(l)):
			if l[i] - l[i - 1] == 2:
				m1.append(l[i - 1])
				m1.append(l[i])
				return m1
		else:
			return [-1]


class Solution:

	def primeTwins(self, L, R):

		def prime(n):
			if n == 1 or n == 0:
				return False
			for i in range(2, n // 2 + 1):
				if n % i == 0:
					return False
			else:
				return True
		l = []
		for i in range(L, R + 1):
			if prime(i):
				l.append(i)
		if len(l) == 1 or len(l) == 0:
			return [-1]
		f = l[0]
		for i in l:
			if i - f == 2:
				return [f, i]
			else:
				f = i
		else:
			return [-1]


class Solution:

	def primeTwins(self, L, R):
		u = R + 1
		primes = [1] * u
		a = []

		def Seive(self, u):
			for i in range(u):
				primes[i] = 'true'
			primes[0] = primes[1] = 'False'
			i = 2
			while i * i <= u:
				if primes[i] == 'true':
					j = i * i
					while j < u:
						primes[j] = 'False'
						j += i
				i += 1
			return primes
		Seive(self, u)
		for i in range(u):
			if primes[i] == 'true':
				a.append(i)
		for j in range(len(a) - 1):
			if a[j + 1] - a[j] == 2 and a[j] >= L and (a[j + 1] <= R):
				return [a[j], a[j + 1]]
		return [-1]


class Solution:

	def primeTwins(self, L, R):
		n = R + 3
		a = [1] * n

		def seive():
			a[0] = a[1] = 0
			i = 2
			while i * i <= n:
				for j in range(i * i, n, i):
					a[j] = 0
				i += 1
			return a
		seive()
		x = []
		for i in range(L, R + 1):
			if a[i] == 1:
				x.append(i)
		if len(x) < 2:
			return [-1]
		for j in range(1, len(x)):
			if x[j] - x[j - 1] == 2:
				return [x[j - 1], x[j]]
		return [-1]


import math

class Solution:

	def primeTwins(self, L, R):
		c = []
		a = []
		s = 0

		def prime(n):
			if n == 1 or n == 0:
				return False
			for i in range(2, int(math.sqrt(n) + 1)):
				if n % i == 0:
					return False
			return True
		for i in range(L, R + 1):
			if prime(i):
				c.append(i)
		for i in range(len(c) - 1):
			if c[i + 1] - c[i] == 2:
				a.append(c[i])
				a.append(c[i + 1])
				s = 1
				break
		if s == 1:
			return a
		else:
			return [-1]


class Solution:

	def primeTwins(self, L, R):
		import math as m

		def isprime(n):
			sq = int(m.sqrt(n))
			for i in range(2, sq + 1):
				if n % i == 0:
					return False
			else:
				return True
		l = []
		for i in range(L, R + 1):
			if isprime(i):
				l.append(i)
		ans = []
		if len(l) == 0:
			return [-1]
		else:
			status = False
			for i in range(len(l) - 1):
				if abs(l[i] - l[i + 1]) == 2:
					ans.append(l[i])
					ans.append(l[i + 1])
					status = True
					break
			if status:
				return ans
			else:
				return [-1]


class Solution:

	def primeTwins(self, L, R):
		l = [1 for i in range(R + 1)]
		p = 2
		while p * p <= R:
			if l[p] == 1:
				for i in range(p * p, R + 1, p):
					l[i] = 0
			p += 1
		d = []
		for i in range(L, R + 1):
			if l[i] == 1:
				d.append(i)
		for i in range(len(d) - 1):
			if d[i + 1] - d[i] == 2:
				return [d[i], d[i + 1]]
		return [-1]


class Solution:

	def primeTwins(self, L, R):
		import math
		c = 0
		b = []

		def prime(x):
			for j in range(2, int(math.sqrt(x)) + 1):
				if x % j == 0:
					return False
			else:
				return True
		a = []
		for i in range(L, R + 1):
			if prime(i) == True:
				a.append(i)
		for i in range(len(a) - 1):
			if a[i + 1] - a[i] == 2:
				b.append(a[i])
				b.append(a[i + 1])
				c = 1
				break
		if c == 1:
			return b
		else:
			return [-1]


class Solution:

	def primeTwins(self, L, R):
		c = [True] * (R + 1)
		p = 2
		b = L // 2
		while p * p <= R:
			if c[p] == True:
				for i in range(p * b, R + 1, p):
					if i != p:
						c[i] = False
			p = p + 1
			b = L // p
		a = [-1]
		for i in range(L, len(c) - 2):
			if i == 1:
				continue
			if c[i] == True and c[i + 2] == True:
				a = [i, i + 2]
				break
		return a


def checkPrime(value):
	if value == 2:
		return True
	else:
		k = 2
		while k ** k <= value:
			if value % k == 0:
				return False
			k += 1
	return True

def printtwins(L, list):
	for i in range(L, len(list)):
		if list[i] == True and i + 2 < len(list) and (list[i + 2] == True):
			return [i, i + 2]
	return [-1]

def seive(L, number):
	number += 1
	list = [True for i in range(0, number)]
	list[0] = False
	list[1] = False
	for i in range(4, number, 2):
		list[i] = False
	for i in range(3, number, 2):
		if list[i] == True:
			if checkPrime(i) == True:
				val = 2
				while val * i < number:
					list[val * i] = False
					val += 1
		else:
			continue
	return printtwins(L, list)

class Solution:

	def primeTwins(self, L, R):
		return seive(L, R)


class Solution:

	def primeTwins(self, L, R):
		sieve = [1] * 100005
		sieve[0] = 0
		sieve[1] = 0
		for i in range(2, int(100005 ** 0.5) + 1):
			if sieve[i] == 1:
				for j in range(i * i, 100005, i):
					if sieve[j] == 1:
						sieve[j] = 0
		primes = []
		for i in range(2, int(R ** 0.5) + 1):
			if sieve[i] == 1:
				primes.append(i)
		temp = [1] * (R - L + 1)
		if L < 2:
			temp[0] = 0
			temp[1] = 0
		for pr in primes:
			first = L // pr * pr
			if first < L:
				first += pr
			if first in primes:
				first += pr
			if first == 0:
				first += pr + pr
			for j in range(first, R + 1, pr):
				temp[j - L] = 0
		final = []
		for i in range(R - L - 1):
			if temp[i] == 1:
				if temp[i + 2] == 1:
					final.append(L + i)
					final.append(L + i + 2)
					break
		if len(final) > 0:
			return final
		else:
			return [-1]