Cerinta completa
You are given non-negative integers, . We define the score for some permutation () of length to be the maximum of for .
Find the permutation with the minimum possible score and print its score.
Note: is the exclusive-OR (XOR) operator.
Input Format
The first line contains single integer, , denoting the number of integers.
The second line contains space-separated integers, , describing the respective integers.
Constraints
Output Format
Print a single integer denoting the minimum possible score.
Sample Input 0
4
1 2 3 4
Sample Output 0
5
Sample Input 1
3
1 2 3
Sample Output 1
2
Explanation
Sample Case 0:
The permutation with the minimum score is :
Because the permutation’s score is the maximum of these values, we print on a new line.
Sample Case 1:
The permutation with the minimum score is :
Because the permutation’s score is the maximum of these values, we print on a new line.
Limbajul de programare folosit: python3
Cod:
#!/bin/python3
import sys
from math import inf
def dec_to_bin(number):
return bin(number)[2:]
def bin_to_dec(number):
return int(number, 2)
def buildup_to_len(number, length):
return '0' * (length - len(number)) + number
def get_max_len(arr):
return len(dec_to_bin(max(arr)))
def get_min_len(arr):
return len(dec_to_bin(min(arr)))
def anotherMinimaxProblem(a):
res = inf
arr_bin = []
if max(a) == min(a) and max(a) == 0:
return 0
for el in a:
arr_bin.append(dec_to_bin(el))
# trim the elements
while len(max(arr_bin, key=len)) == len(min(arr_bin, key=len)):
arr_bin = [ el[:1].lstrip('1') + el[1:] for el in arr_bin ]
arr_bin = [ el.lstrip('0') for el in arr_bin ]
# build up the lesser elements
max_len = len(max(arr_bin, key=len))
#print("max len = {}".format(max_len))
arr_bin = [ buildup_to_len(el, max_len) for el in arr_bin ]
arr_zeros = []
arr_ones = []
for el in sorted(arr_bin):
if el[0] == '0':
arr_zeros.append(el)
else:
arr_ones.append(el)
for el_z in arr_zeros:
for el_o in arr_ones:
res = min(res, bin_to_dec(el_z) ^ bin_to_dec(el_o))
return res
if __name__ == "__main__":
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)
Scor obtinut: 1.0
Submission ID: 464605090
Link challenge: https://www.hackerrank.com/challenges/yet-another-minimax-problem/problem
