1374D - Zero Remainder Array - CodeForces Solution


math sortings two pointers *1400

Please click on ads to support us..

Python Code:

from collections import Counter
for _ in range(int(input())):
    n,k=map(int,input().split())
    arr=list(map(int,input().split()))
    d=Counter()
    for i in arr:
        if i%k!=0:
            d[k-i%k]+=1
    ma=0
    for i in d:
        ma=max(ma,i+d[i]*k-k+1)
    print(ma)

C++ Code:

#include<bits/stdc++.h>
using namespace std ;
 
#define ll long long 

int main(){
    
    int t; cin>>t ; 
    while(t--){
        
        ll n,k ; cin>>n>>k ; 
        ll arr[n] ; 
        for(int i=0; i<n; i++) {
            cin>>arr[i] ; 
            arr[i] = k - arr[i]%k ;  
            if(arr[i]==k){
                arr[i]=0 ; 
            }
        }
        
        sort(arr,arr+n) ; 
        ll ans = 0 ; 
        map<ll,ll>mp ; 
        
        for(int i=0; i<n; i++){
            if(arr[i]!=0 && mp[arr[i]]==0){
                mp[arr[i]] = arr[i] ; 
                ans = max(ans,arr[i]) ; 
            }
            else if(arr[i]!=0 && mp[arr[i]]!=0){
                ans = max(ans,k+mp[arr[i]]) ; 
                mp[arr[i]] += k ; 
            }
        }  
        
        if(ans==0){
            ans=-1 ; 
        }
        
        cout<<ans+1<<endl;              
    }
}


Comments

Submit
0 Comments
More Questions

1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence