Chef has an array A of length N.

In one operation, Chef can choose any two distinct indices i, j (1 ≤ i, j ≤ N, i \neq j) and either change A_{i} to A_{j} or change A_{j} to A_{i}.

Find the minimum number of operations required to make all the elements of the array equal.

------ Input Format ------ 

- First line will contain T, number of test cases. Then the test cases follow.
- First line of each test case consists of an integer N - denoting the size of array A.
- Second line of each test case consists of N space-separated integers A_{1}, A_{2}, \dots, A_{N} - denoting the array A.

------ Output Format ------ 

For each test case, output the minimum number of operations required to make all the elements equal.

------ Constraints ------ 

$1 ≤ T ≤ 100$
$2 ≤ N ≤ 1000$
$1 ≤ A_{i} ≤ 1000$

----- Sample Input 1 ------ 
4
3
1 2 3
4
5 5 5 5
4
2 2 1 1
3
1 1 2

----- Sample Output 1 ------ 
2
0
2
1

----- explanation 1 ------ 
Test Case $1$: You can make all the elements equal in $2$ operations. In the first operation, you can choose indices $1, 2$ and convert $A_{1}$ to $A_{2}$. So the array becomes $[2, 2, 3]$. Now you can choose indices $1, 3$ and convert $A_{3}$ to $A_{1}$, so the final array becomes $[2, 2, 2]$.

Test Case $2$: Since all the elements are already equal there is no need to perform any operation.

Test Case $3$: You can make all the elements equal in $2$ operations. In the first operation, you can choose indices $1, 3$ and convert $A_{1}$ to $A_{3}$. So the array becomes $[1, 2, 1, 1]$. Now you can choose indices $1, 2$ and convert $A_{2}$ to $A_{1}$, so the final array becomes $[1, 1, 1, 1]$.

Test Case $4$: You can make all the elements equal in $1$ operation. You can pick indices $2, 3$ and convert $A_{3}$ to $A_{2}$ after which the array becomes $[1, 1, 1]$.

Difficulty: EASY
Tags: Data structures
Skill types: Data structures

Starter code:

Solutions:
from statistics import mode
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	count = 0
	m = mode(a)
	for i in range(n):
		if a[i] != m:
			count += 1
	print(count)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(n - max(Counter(l).values()))


T = int(input())
for _ in range(T):
	N = int(input())
	lst = [int(i) for i in input().split()][0:N]
	set_lst = set(lst)
	dict_count = {}
	for e in set_lst:
		dict_count[e] = lst.count(e)
	sorted_value = sorted(dict_count.items(), key=lambda x: x[1])
	max_freq = sorted_value[len(set_lst) - 1][1]
	count = len(lst) - max_freq
	print(count)


T = int(input())
for z in range(T):
	N = int(input())
	A = list(map(int, input().strip().split()))
	li = []
	for i in A:
		s = A.count(i)
		li.append(s)
	print(N - max(li))


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	l1 = []
	for i in A:
		a = A.count(i)
		l1.append(a)
	print(N - max(l1))


def maeuP(arr, n):
	dct = dict()
	for _ in arr:
		if _ in dct:
			dct[_] += 1
		else:
			dct[_] = 1
	max_r = max(dct.values())
	print(n - max_r)
for i in range(int(input())):
	N = int(input())
	array = list(map(int, input().split()))[:N]
	maeuP(array, N)


from collections import Counter
t = int(input())
for j in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	d = Counter(l)
	v = max(d.values())
	print(n - v)


from statistics import mode
n = int(input())
for _ in range(n):
	N = int(input())
	ARR = list(map(int, input().split()))
	m = mode(ARR)
	print(N - ARR.count(m))


n = int(input())
for _ in range(n):
	m = []
	n = int(input())
	a = list(map(int, input().split(' ')))
	for i in a:
		s = a.count(i)
		m.append(s)
	print(n - max(m))


t = int(input())
for i in range(t):
	m = []
	n = int(input())
	l = list(map(int, input().split()))
	for i in l:
		a = l.count(i)
		m.append(a)
	print(n - max(m))


from statistics import mode
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = mode(a)
	print(len(a) - a.count(m))


t = int(input())
for i in range(t):
	n = int(input())
	a = map(int, input().split())
	dict = {}
	for j in a:
		if j in dict:
			dict[j] += 1
		else:
			dict[j] = 1
	value = max(dict.values())
	print(n - value)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	ls = list(map(int, input().split()))
	maxn = max(Counter(ls).values())
	print(n - maxn)


t = int(input())
for i in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))[:n]
	l1 = []
	for i in l:
		l1.append(l.count(i))
	a = l1.index(max(l1))
	c = l.count(l[a])
	print(n - c)


from collections import Counter
n = int(input())
for _ in range(n):
	l = int(input())
	s = map(int, input().split())
	print(l - max(Counter(s).values()))


from statistics import mode
T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	print(N - A.count(mode(A)))


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	freq = {}
	for elem in x:
		if elem in freq:
			freq[elem] += 1
		else:
			freq[elem] = 1
	max_freq = max(freq.values())
	print(n - max_freq)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	most_common = max(set(a), key=a.count)
	count = sum((1 for elem in a if elem != most_common))
	print(count)


from collections import Counter
T = int(input())
for i in range(T):
	N = int(input())
	list1 = input().split()
	a = max(Counter(list1).values())
	print(N - a)


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	l = []
	for i in lst:
		c = lst.count(i)
		if c == n:
			print(0)
			break
		else:
			l.append(c)
	if len(l) != 0:
		max_value = max(l)
		print(n - max_value)


t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	c = []
	for i in range(n):
		c = [a.count(a[i])] + c
	z = str(c)
	p = int(max(c))
	print(n - p)
	t = t - 1


from collections import Counter
t = int(input())
for _ in range(t):
	N = int(input())
	list1 = input().split()
	a = max(Counter(list1).values())
	print(N - a)


t = int(input())
for i in range(t):
	k = []
	n = int(input())
	a = list(map(int, input().split(' ')))
	for i in a:
		s = a.count(i)
		k.append(s)
	print(n - max(k))


for i in range(0, int(input())):
	x = int(input())
	a = list(map(int, input().split()))
	l = []
	for i in range(0, len(a)):
		y = a.count(a[i])
		l.append(y)
	c = max(l)
	print(len(a) - c)


from statistics import mode
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(n - a.count(mode(a)))


n = int(input())
while n != 0:
	x = int(input())
	l = list(map(int, input().split()))
	m = []
	for i in range(0, len(l)):
		c = l.count(l[i])
		m.append(c)
	m.sort()
	k = len(m)
	s = m[k - 1]
	print(k - s)
	n = n - 1


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for j in l:
		ans = max(ans, l.count(j))
	print(n - ans)


from collections import Counter
n = int(input())
a = []
for i in range(n):
	count = 0
	q = int(input())
	w = list(map(int, input().split()))
	cout = Counter(w)
	m = cout.most_common(1)[0][0]
	for j in range(len(w)):
		if w[j] != m:
			count += 1
	print(count)


n = int(input())
for i in range(n):
	a = int(input())
	l = list(map(int, input().split()))
	d = []
	for j in range(a):
		c = l.count(l[j])
		d.append(c)
	print(a - max(d))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = []
	m = 0
	for i in l:
		m = max(m, l.count(i))
	print(len(l) - m)


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	C = Counter(arr)
	maxi = max(C.values())
	print(n - maxi)


from collections import Counter
cases = int(input())
for i in range(cases):
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	result = n - max(Counter(arr).values())
	print(result)


T = int(input())
for j in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	List1 = []
	c = 0
	for i in range(N):
		if A[i] == A[i - 1]:
			continue
		counter = A.count(A[i])
		List1.append(counter)
	if List1 == []:
		print(0)
	else:
		print(N - max(List1))


import statistics as st
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	m = st.mode(arr)
	print(len(arr) - arr.count(m))


t = int(input())
for i in range(t):
	z = 0
	n = int(input())
	l = list(map(int, input().split()))
	ls = max(set(l), key=l.count)
	z = l.count(ls)
	m = len(l)
	print(m - z)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	dic = {}
	i = 0
	m = 0
	while i < n:
		if arr[i] not in dic:
			dic[arr[i]] = 1
		else:
			dic[arr[i]] += 1
		i += 1
	for (k, v) in dic.items():
		if v >= m:
			m = v
	print(n - m)


from collections import Counter
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))[:n]
	count = Counter(a)
	max1 = max(count.values())
	print(n - max1)


from collections import Counter
T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	print(N - max(Counter(A).values()))


t = int(input())
for i in range(t):
	n = int(input())
	a = input().split()
	d = {}
	for j in a:
		if j not in d:
			d[j] = 0
		d[j] += 1
	lst = list(d.values())
	lst.sort()
	print(n - lst[-1])


t = int(input())
for i in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	mydict = {}
	for num in A:
		if num in mydict:
			mydict[num] += 1
		else:
			mydict[num] = 1
	maxv = max(mydict.values())
	print(n - maxv)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = []
	for j in a:
		x.append(a.count(j))
	m = max(x)
	print(len(a) - m)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	p = set(a)
	count = 1
	maxc = 1
	for i in p:
		count = a.count(i)
		if count > maxc:
			maxc = count
	print(n - maxc)


for _ in range(int(input())):
	int(input())
	ls = list(map(int, input().split()))
	max_count = 0
	for i in ls:
		ct = ls.count(i)
		if ct > max_count:
			max_count = ct
	swaps = len(ls) - max_count
	print(swaps)


from statistics import mode
n = eval(input())
for i in range(n):
	a = eval(input())
	b = list(map(int, input().split()))
	x = mode(b)
	y = len(b) - b.count(x)
	print(y)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	freq = {}
	for num in a:
		freq[num] = freq.get(num, 0) + 1
	max_freq = max(freq.values())
	operations = n - max_freq
	print(operations)


from statistics import mode
for _ in range(int(input())):
	i_p = int(input())
	lis = list(map(int, input().split()))
	most = mode(lis)
	print(len(lis) - lis.count(most))


n = int(input())
for i in range(n):
	l = int(input())
	a = input().split()
	uni = {}
	for x in a:
		if x not in uni:
			uni[x] = 1
		else:
			uni[x] += 1
	print(len(a) - max(uni.values()))


t = int(input())
for i in range(t):
	size = int(input())
	n = list(map(int, input().split()))
	if len(set(n)) == 1:
		print(0)
	else:
		mx = 0
		for j in n:
			mx = max(mx, n.count(j))
		print(len(n) - mx)


for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if len(set(arr)) == 1:
		print(0)
	else:
		mx = 0
		for a in arr:
			mx = max(mx, arr.count(a))
		print(len(arr) - mx)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	unique = set(a)
	freq = dict()
	for val in unique:
		freq[val] = 0
	for val in a:
		freq[val] += 1
	max_ = max(freq.values())
	print(n - max_)


T = int(input())
for i in range(T):
	N = int(input())
	A = [int(x) for x in input().split()]
	l1 = []
	for j in A:
		a = A.count(j)
		l1.append(a)
		c = len(A)
	l1.sort()
	if l1[-1] > 1:
		print(c - l1[-1])
	elif l1[-1] == 1:
		print(c - 1)


t = int(input())
for i in range(t):
	a = int(input())
	sum = 1
	l = list(map(int, input().split()))
	for i in l:
		b = l.count(i)
		if b > sum:
			sum = b
	print(a - sum)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = []
	g = len(l)
	for i in l:
		s = l.count(i)
		m.append(s)
	print(g - max(m))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	count_no = []
	for j in range(n):
		a = arr.count(arr[j])
		count_no.append(a)
	no = max(count_no)
	print(n - no)


for i in range(int(input())):
	a = int(input())
	l = list(map(int, input().split()))
	c = l.count(l[0])
	for i in l:
		if c < l.count(i):
			c = l.count(i)
	print(a - c)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = []
	for i in a:
		b = a.count(i)
		c.append(b)
	print(n - max(c))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	counts = {}
	for j in a:
		if j in counts:
			counts[j] += 1
		else:
			counts[j] = 1
	print(n - max(counts.values()))


for i in range(int(input())):
	l = int(input())
	l1 = list(map(int, input().split()))
	m = []
	for i in l1:
		a = l1.count(i)
		m.append(a)
	print(l - max(m))


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	z = []
	for i in a:
		z.append(a.count(i))
	print(n - max(z))


import numpy
from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	arr = numpy.array(input().split())
	p = Counter(arr)
	x = max(p.values())
	print(n - x)