Maximize It!

Sort by

recency

|

1092 Discussions

|

  • + 0 comments
    from itertools import product
    
    numbers = {}
    S = 0
    
    K, M = map(int, input().split())
    
    for i in range(K):
        _, *numbers[i] = map(int, input().split())
    
    S = max(S, max(sum(x ** 2 for x in combo) % M for combo in product(*numbers.values())))
    
    print(S)
    
  • + 0 comments
    from itertools import product
    
    def f(X,m):
        result = 0
        for element in X:
            result += element**2
        return result%m
        
        
    def get_max_combo():
        num_lists, m = map(int, input().split()) # Read
        lists = [] # initialize where to save in the lists
        # save lists from input into lists
        for i in range(num_lists): 
            num_elements, *elements = map(int, input().split())
            lists.append(elements)
        # create combinations of every possible list entry with entries from other list entries 
        combos = list(product(*lists))
        max_f = 0
        for combo in combos:
            current_max = f(combo, m)
            if current_max > max_f:
                max_f = current_max
        print(max_f)
        
        
    get_max_combo()
    
  • + 0 comments

    The most common error is that each line starts with the number of subsequent elements on the line (this is quite uncommon to have it on the same line in HR challenges)!

    The shortest code I found in Python (not the fastest, it is faster to check the max each time in the loop):

    from itertools import product
    
    K, M = (int(x) for x in input().split())
    print(max(sum(t)%M for t in product(*[[int(x)**2 for x in input().split()][1:] for _ in range(K)])))
    
  • + 0 comments

    was a bit tough and fun

    from itertools import product
    
    k, m = map(int, input().split())
    L, max_value = [], 0
    for i in range(0, k):
        L.append(list(map(lambda x: int(x) ** 2, input().split()[1:])))
    
    for i in product(*L):
        if max_value < (sum(i) % m):
            max_value = sum(i) % m
    print(max_value)
    
  • + 1 comment

    unable to pass 4 test cases any help in logic would be appriciated

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import itertools
    k_n_list=input().split()  #take K,M as input
    number=int(k_n_list[0])   # getting number of K
    new_list=[]               # empty list 
    find_element=[]           # empty list to store all possible squared modulo operated value
    for i in range(0,number):   # for loop for appending the number of inputed list
        new_list.append(input().split())  # nested list of lists
    combinations = list(itertools.product(*new_list))  # all possible values of the nested list
    # it dose not conatain the same element from the list 
    for comb in combinations: # square and modulo operation in this loop
        b =list((map(int, comb)))
        out=sum(map(lambda i: i*i , b))% int(k_n_list[1])
        find_element.append(out)
    print(max(find_element))