We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
TimeComplexity:O(N^2)SpaceComplexity:O(N)fromcollectionsimportOrderedDictdefdecrement(V,val):'''Decrementthevalueandremoveifthecountdropstozero.'''V[val]-=1ifV[val]==0:delV[val]defdel_rec(cur_i,max_i,cnt,cur_sum,S,K,V,A):'''Recursivefunctiontohelpadjustthesequence.'''ifcnt==K:decrement(V,cur_sum)else:del_rec(cur_i,max_i,cnt+1,cur_sum+A[cur_i],S,K,V,A)ifcur_i<max_i:del_rec(cur_i+1,max_i,cnt,cur_sum,S,K,V,A)T=int(input().strip())for_inrange(T):N,K=map(int,input().strip().split())S=sorted(map(int,input().strip().split()))# Initialize ordered dictionary to track occurrencesV=OrderedDict()forxinS:ifxnotinV:V[x]=1else:V[x]+=1#array A using first element division logicA=[S[0]// K] + [0] * (N - 1)decrement(V,S[0])#Decreasethecountofthefirstelement# subsequent elements of Aforninrange(1,N):A[n]=list(V.keys())[0]-(K-1)*A[0]ifn<N-1:del_rec(0,n,1,A[n],S,K,V,A)print(' '.join(map(str,A)))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Repetitive K-Sums
You are viewing a single comment's thread. Return to all comments →
Python Hybrid Algo Approach