forked from hansrajdas/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_unique_array_sum.py
62 lines (52 loc) · 1.39 KB
/
min_unique_array_sum.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
#!/usr/bin/python
# Date: 2018-10-15
#
# Description:
# Given an array, increment any duplicate elements until all its elements are
# unique. In addition, the sum of its elements must be the minimum possible
# within the rules.
# For example if arr = [3, 2, 1, 2, 7], then arrMin will be [3, 2, 1, 4, 7]
# and its elements sum to minimal value of 3 + 2 + 1 + 4 + 7 = 17
#
# Approach:
# Take an empty set and scan array from beginning, if array element is present
# in set increment that element until we find an element which is not present in
# set and add that to set.
# Return sum of all set elements.
#
# Complexity:
# O(N) average case, O(N^2) in worst case
# N = Number of elements in array
MAX_VALUE = 5001
def getMinimumUniqueSum(arr):
unique = set()
for a in arr:
for x in range(a, MAX_VALUE):
if x not in unique:
break
unique.add(x)
return sum(unique)
def main():
a = []
n = input('Enter number of elements: ')
for i in xrange (n):
x = input('Enter value of a[%d]: ' % i)
a.append(x)
print getMinimumUniqueSum(a)
if __name__ == '__main__':
main()
# Output:
# ------------------------
# Enter number of elements: 3
# Enter value of a[0]: 1
# Enter value of a[1]: 2
# Enter value of a[2]: 2
# 6
#
# Enter number of elements: 5
# Enter value of a[0]: 3
# Enter value of a[1]: 2
# Enter value of a[2]: 1
# Enter value of a[3]: 2
# Enter value of a[4]: 7
# 17