-
Notifications
You must be signed in to change notification settings - Fork 1
/
EqualizeTheArrayHR.py
63 lines (41 loc) · 1.66 KB
/
EqualizeTheArrayHR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Karl has an array of integers. He wants to reduce the array until all remaining elements are equal. Determine the minimum number of elements to delete to reach his goal.
For example, if his array is , we see that he can delete the elements and leaving . He could also delete both twos and either the or the , but that would take deletions. The minimum number of deletions is .
Function Description
Complete the equalizeArray function in the editor below. It must return an integer that denotes the minimum number of deletions required.
equalizeArray has the following parameter(s):
arr: an array of integers
Input Format
The first line contains an integer , the number of elements in .
The next line contains space-separated integers .
Constraints
Output Format
Print a single integer that denotes the minimum number of elements Karl must delete for all elements in the array to be equal.
Sample Input
5
3 3 2 1 3
Sample Output
2
Explanation
Array . If we delete and , all of the elements in the resulting array, , will be equal. Deleting these elements is minimal. Our only other options would be to delete elements to get an array of either or .
"""
# SOLUTION
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
# Complete the equalizeArray function below.
def equalizeArray(arr):
d = Counter(arr).values()
deletion = len(arr) - max(d)
return deletion
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
result = equalizeArray(arr)
fptr.write(str(result) + '\n')
fptr.close()