• + 1 comment
    function nonDivisibleSubset(k, s) {
        let remFreq = {}; 
        let ans = 0; 
        
        for(let n of s) {
            remFreq[n % k] = (remFreq[n % k] || 0 )+ 1; 
        }
        
        // take a single number from all those numbers having rem 0
        
        if('0' in remFreq) {
            ans += 1; 
        }
        
        // next add up the count of remainder keeping in mind two edge cases 
        
        for(let rem = 1; rem <= Math.floor(k / 2); rem++) {
            // take a single number from all having remainder k / 2
            if(rem === k - rem) {
                ans += 1; 
            }
            else { // take the max of rem or k - rem 
                ans += Math.max(remFreq[rem] || 0, remFreq[k - rem] || 0); 
            }
        }
        
        return ans; 
    }