Ram and Rohan are participating in a tournament. Together they must face off against x contenders whose strengths are given in an array arr[]. The respective strength of Ram and Rohan in m and n. They take turns taking part in face-off matches with contenders and Ram goes first. They win a match only if their opponent's strength is an integral multiple of their strengths. If the opponent is defeated, he can not fight again. 
Find out who among Ram and Rohan is a better player. If both of them win the same number of matches then both are winners.
Example 1:
Input: 
x = 7
m = 2, n = 3
arr = {4, 5, 7, 6, 9, 10, 14}
Output: Ram
Explaination: Ram win against opponents 
having strength 4, 6, 10, 14 and Rohan 
win against opponent having strength 9.
Example 2:
Input: 
x = 6
m = 5, n = 3
arr = {1, 9, 9, 10, 9, 18}
Output: Rohan
Explaination: Ram win against opponent 
having strength 10 and Rohan win against 
three opponents having strength 9 and one 
having strength 18.
Your Task:
You do not need to read input or print anything. Your task is to complete the function winner() which takes x, m, n and arr as input parameters and returns one of the strings from {"Ram", "Rohan", "Both"} based on the answer. 
Expected Time Complexity: O(x)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ x, n, m ≤ 10^{5}
1 ≤ arr[i] ≤ 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def winner(self, x, m, n, arr):

        # code here

Solutions:
class Solution:

	def winner(self, x, m, n, arr):
		rams = 0
		rohan = 0
		for i in arr:
			if i % m == 0:
				rams += 1
		for i in arr:
			if i % n == 0 and i % m != 0:
				rohan += 1
		if rams > rohan:
			return 'Ram'
		elif rohan > rams:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ram = [num for num in arr if num % m == 0]
		rohan = [num for num in arr if num % n == 0 and num not in ram]
		a = len(ram)
		b = len(rohan)
		if a > b:
			return 'Ram'
		elif a == b:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		a = 0
		b = 0
		for i in range(x):
			if arr[i] % m == 0:
				a += 1
			elif arr[i] % n == 0:
				b += 1
		if a == b:
			return 'Both'
		if a > b:
			return 'Ram'
		if a < b:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		ok = 0
		kl = 0
		for i in range(x):
			if arr[i] % m == 0:
				ok += 1
			elif arr[i] % n == 0:
				kl += 1
		if ok > kl:
			return 'Ram'
		elif ok < kl:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ramWinCount = 0
		rohanWinCount = 0
		for i in arr:
			if i % m == 0:
				ramWinCount = ramWinCount + 1
			elif i % n == 0:
				rohanWinCount = rohanWinCount + 1
		ans = 'Ram' if ramWinCount > rohanWinCount else 'Rohan' if ramWinCount < rohanWinCount else 'Both'
		return ans


class Solution:

	def winner(self, x, m, n, arr):
		count_m = 0
		count_n = 0
		for i in range(x):
			if arr[i] % m == 0:
				count_m += 1
		for i in range(x):
			if arr[i] % n == 0 and arr[i] % m != 0:
				count_n += 1
		if count_m > count_n:
			return 'Ram'
		elif count_m < count_n:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ram_win = 0
		rohan_win = 0
		for index in range(x):
			if arr[index] % m == 0:
				ram_win += 1
			elif arr[index] % n == 0:
				rohan_win += 1
		if ram_win > rohan_win:
			return 'Ram'
		elif ram_win < rohan_win:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		o = 0
		e = 0
		for i in arr:
			if i % m == 0:
				o = o + 1
			elif i % n == 0:
				e = e + 1
		if o > e:
			return 'Ram'
		elif o == e:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		ram = 0
		rohan = 0
		for i in arr:
			if i % m == 0:
				ram += 1
				arr.remove(i)
		for i in arr:
			if i % n == 0:
				rohan += 1
		if ram > rohan:
			return 'Ram'
		elif ram < rohan:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ram_score = 0
		rohan_score = 0
		for i in arr:
			if i % m == 0:
				ram_score = ram_score + 1
				arr.pop(arr.index(i))
		for i in arr:
			if i % n == 0:
				rohan_score = rohan_score + 1
		if ram_score > rohan_score:
			return 'Ram'
		elif ram_score < rohan_score:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		r = 0
		rh = 0
		for i in range(len(arr)):
			if arr[i] % m == 0:
				r = r + 1
			elif arr[i] % n == 0:
				rh = rh + 1
		if r > rh:
			return 'Ram'
		elif r == rh:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		d = {}
		for i in arr:
			d[i] = 0
		(mc, nc) = (0, 0)
		for i in arr:
			if d[i] != 1:
				if i % m == 0:
					mc += 1
					d[i] = 1
				elif i % n == 0:
					nc += 1
					d[i] = 1
		if mc > nc:
			return 'Ram'
		elif mc < nc:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		arr = list(set(arr))
		ram = 0
		rohan = 0
		for i in range(len(arr)):
			if arr[i] % m == 0:
				ram = ram + 1
			elif arr[i] % n == 0:
				rohan = rohan + 1
		ans = [ram, rohan]
		if ram == rohan:
			return 'Both'
		if max(ans) == ans[0]:
			return 'Ram'
		elif max(ans) == ans[1]:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		ram = []
		rohan = []
		for i in arr:
			if i % m == 0:
				ram.append(i)
			elif i % n == 0:
				rohan.append(i)
		if len(ram) > len(rohan):
			return 'Ram'
		elif len(ram) == len(rohan):
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		c = 0
		l = 0
		for i in range(x):
			if arr[i] % m == 0:
				c = c + 1
			elif arr[i] % n == 0:
				l = l + 1
		if c == l:
			return 'Both'
		if c < l:
			return 'Rohan'
		if c > l:
			return 'Ram'


from math import floor, ceil

class Solution:

	def winner(self, x, m, n, arr):
		ram = 0
		rohan = 0
		ra = []
		for j in arr:
			if floor(j / m) == ceil(j / m):
				ram += 1
				ra.append(j)
		ras = set(ra)
		for j in arr:
			if j not in ras:
				if floor(j / n) == ceil(j / n):
					rohan += 1
		if ram > rohan:
			return 'Ram'
		elif ram == rohan:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		resa = 0
		resb = 0
		for i in range(x):
			if arr[i] % m == 0:
				resa += 1
			elif arr[i] % n == 0:
				resb += 1
		if resa > resb:
			return 'Ram'
		if resb > resa:
			return 'Rohan'
		if resa == resb:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		mscore = 0
		nscore = 0
		for i in range(x):
			if arr[i] % m == 0:
				mscore += 1
			elif arr[i] % n == 0:
				nscore += 1
		if mscore > nscore:
			return 'Ram'
		elif nscore > mscore:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ramcount = 0
		rohcount = 0
		for i in arr:
			if i % m == 0:
				ramcount += 1
			elif i % n == 0:
				rohcount += 1
		if ramcount == rohcount:
			return 'Both'
		if ramcount > rohcount:
			return 'Ram'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		m_c = 0
		n_c = 0
		for elem in arr:
			if elem % m == 0:
				m_c += 1
			elif elem % n == 0:
				n_c += 1
		if m_c > n_c:
			return 'Ram'
		elif m_c < n_c:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		(count1, count2) = (0, 0)
		for i in range(x):
			if arr[i] % m == 0:
				count1 += 1
			elif arr[i] % n == 0:
				count2 += 1
		if count1 > count2:
			return 'Ram'
		elif count1 == count2:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		r = 0
		l = 0
		for i in arr:
			if i % m == 0:
				r += 1
		for i in arr:
			if i % n == 0 and i % m != 0:
				l += 1
		if r > l:
			return 'Ram'
		elif l > r:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		win_r = 0
		win_ro = 0
		for i in range(len(arr)):
			if arr[i] % m == 0:
				win_r += 1
		for i in range(len(arr)):
			if arr[i] % n == 0 and arr[i] % m != 0:
				win_ro += 1
		if win_r > win_ro:
			return 'Ram'
		elif win_ro > win_r:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		count_ram = 0
		count_rohan = 0
		for i in range(x):
			if arr[i] % m == 0:
				count_ram += 1
			elif arr[i] % n == 0:
				count_rohan += 1
		if count_ram > count_rohan:
			return 'Ram'
		elif count_rohan > count_ram:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		r1 = 0
		ro1 = 0
		for i in arr:
			if i % m == 0:
				r1 += 1
			elif i % n == 0:
				ro1 += 1
		if r1 == ro1:
			return 'Both'
		elif r1 > ro1:
			return 'Ram'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		s = set()
		for i in range(x):
			s.add(arr[i])
		arr2 = list(s)
		itr = len(arr2)
		count = 0
		count1 = 0
		for i in range(itr):
			if arr2[i] % m == 0:
				count += 1
			elif arr2[i] % n == 0:
				count1 += 1
		if count > count1:
			return 'Ram'
		elif count == count1:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		cram = 0
		crohan = 0
		for y in arr:
			if y % m == 0:
				cram += 1
			elif y % n == 0:
				crohan += 1
		if crohan == cram:
			return 'Both'
		elif cram > crohan:
			return 'Ram'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		c = 0
		d = 0
		flag = True
		for i in arr:
			if i % m == 0:
				c += 1
				flag = False
			elif i % n == 0:
				d += 1
				flag = True
		if c == d:
			return 'Both'
		elif d > c:
			return 'Rohan'
		else:
			return 'Ram'


class Solution:

	def winner(self, x, m, n, arr):
		r = 0
		s = 0
		l = [0] * x
		for i in range(x):
			if arr[i] % m == 0:
				r += 1
				l[i] = 1
		for i in range(x):
			if arr[i] % n == 0 and l[i] == 0:
				s += 1
		if r > s:
			return 'Ram'
		if s > r:
			return 'Rohan'
		return 'Both'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		x = int(input())
		(m, n) = [int(a) for a in input().split()]
		arr = input().split()
		for i in range(x):
			arr[i] = int(arr[i])
		ob = Solution()
		print(ob.winner(x, m, n, arr))


class Solution:

	def winner(self, x, m, n, arr):
		m_count = 0
		n_count = 0
		for i in range(0, x):
			if arr[i] % m == 0:
				m_count += 1
			elif arr[i] % n == 0:
				n_count += 1
		if m_count > n_count:
			return 'Ram'
		elif n_count > m_count:
			return 'Rohan'
		else:
			return 'Both'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		x = int(input())
		(m, n) = [int(a) for a in input().split()]
		arr = input().split()
		for i in range(x):
			arr[i] = int(arr[i])
		ob = Solution()
		print(ob.winner(x, m, n, arr))


class Solution:

	def winner(self, x, m, n, arr):
		count1 = 0
		count2 = 0
		itr = 0
		while itr < len(arr):
			if arr[itr] % m == 0:
				count1 += 1
				arr.pop(itr)
			else:
				itr += 1
		for itr in range(len(arr)):
			if arr[itr] % n == 0:
				count2 += 1
		if count1 > count2:
			return 'Ram'
		elif count1 < count2:
			return 'Rohan'
		else:
			return 'Both'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		x = int(input())
		(m, n) = [int(a) for a in input().split()]
		arr = input().split()
		for i in range(x):
			arr[i] = int(arr[i])
		ob = Solution()
		print(ob.winner(x, m, n, arr))


class Solution:

	def winner(self, x, m, n, arr):
		(countm, countn) = (0, 0)
		for i in range(x):
			if arr[i] % m == 0:
				countm += 1
			elif arr[i] % n == 0:
				countn += 1
		if countn == countm:
			return 'Both'
		elif countn > countm:
			return 'Rohan'
		else:
			return 'Ram'


class Solution:

	def winner(self, x, m, n, arr):
		rcount = 0
		rocount = 0
		for i in arr:
			if i % m == 0:
				rcount += 1
			elif i % n == 0:
				rocount += 1
			else:
				pass
		if rcount > rocount:
			return 'Ram'
		elif rcount == rocount:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		ram_c = 0
		rohan_c = 0
		for val in arr:
			if val % m == 0:
				ram_c += 1
			elif val % n == 0:
				rohan_c += 1
		if ram_c > rohan_c:
			return 'Ram'
		elif ram_c < rohan_c:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ra = ro = 0
		for i in arr:
			if i % m == 0:
				ra += 1
			elif i % n == 0:
				ro += 1
		if ra > ro:
			return 'Ram'
		elif ro > ra:
			return 'Rohan'
		else:
			return 'Both'


class Solution:

	def winner(self, x, m, n, arr):
		ram_count = 0
		rohan_count = 0
		for i in range(x):
			if arr[i] % m == 0:
				ram_count += 1
			elif arr[i] % n == 0:
				rohan_count += 1
		if ram_count > rohan_count:
			return 'Ram'
		elif ram_count == rohan_count:
			return 'Both'
		else:
			return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		countRam = 0
		countRoh = 0
		for i in range(x):
			if arr[i] % m == 0:
				countRam += 1
			elif arr[i] % n == 0:
				countRoh += 1
		if countRam > countRoh:
			return 'Ram'
		elif countRam == countRoh:
			return 'Both'
		return 'Rohan'


class Solution:

	def winner(self, x, m, n, arr):
		ramOpp = list(filter(lambda x: x % m == 0, arr))
		ramScore = len(ramOpp)
		for el in ramOpp:
			if el in arr:
				arr.remove(el)
		rohanScore = len(list(filter(lambda x: x % n == 0, arr)))
		if ramScore > rohanScore:
			return 'Ram'
		elif rohanScore > ramScore:
			return 'Rohan'
		else:
			return 'Both'