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.
#!/bin/python3importmathimportosimportrandomimportreimportsysdefnonDivisibleSubset(k,s):# Step 1: Calculate the remainder frequenciesremainder_counts=[0]*kfornumins:remainder_counts[num%k]+=1# Step 2: Initialize the subset sizemax_subset_size=0# Step 3: Include at most one element with remainder 0ifremainder_counts[0]>0:max_subset_size+=1# Step 4: Process remainders from 1 to k//2forrinrange(1,(k// 2) + 1):ifr==k-r:#Specialcasewhereremainderisexactlyhalfofkifremainder_counts[r]>0:max_subset_size+=1else:max_subset_size+=max(remainder_counts[r],remainder_counts[k-r])returnmax_subset_sizeif__name__=='__main__':importsysinput=sys.stdin.readdata=input().split()n=int(data[0])k=int(data[1])s=list(map(int,data[2:n+2]))result=nonDivisibleSubset(k,s)print(result)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Non-Divisible Subset
You are viewing a single comment's thread. Return to all comments →