Sort by

recency

|

818 Discussions

|

  • + 0 comments

    Python3 Solution

    def nonDivisibleSubset(k, s):
        s = [num%k for num in s]
        s = sorted(s)
        
        res = 0
        
        if s[0] == 0:
            res += 1
        
        sDict = {}
        for num in s:
            if num != 0:
                if num in sDict:
                    sDict[num] += 1
                else:
                    sDict[num] = 1
        
        s = list(sDict.items())
     
        l, r = 0, len(s)-1
        
        while l <= r:
            if s[l][0]+s[r][0] == k:
                if s[l][0] == k/2:
                    res += 1
                else:
                    if s[l][1] > s[r][1]:
                        res += s[l][1]
                    else:
                        res += s[r][1]
                r -= 1
                l += 1     
            else:
                if s[l][0]+s[r][0] > k:
                    res += s[r][1]
                    r -= 1
                else:
                    res += s[l][1]
                    l += 1
            
    
        return res
    
  • + 3 comments

    Enjoy smooth HD movie streaming with Desi Cinema Latest Version today. It offers a vast library of Bollywood, South Indian movies. Consequently, lightweight, secure, updated regularly, built for unlimited entertainment fun.

  • + 0 comments

    Here is HackerRank Non-Divisible Subset problem solution in python, java, c++, c and javascript -https://programmingoneonone.com/hackerrank-non-divisible-subset-problem-solution.html

  • + 0 comments

    Solution in Python 3

    #!/bin/python3
    # try a dumb bruteforce
    
    import sys
    from itertools import combinations
    
    def check_array(k, arr):
        for el1 in arr:
            test_arr = list(arr)
            test_arr.remove(el1)
            for el2 in test_arr:
                if (el1 + el2) % k == 0:
                    return False
        return True
    
    def nonDivisibleSubset_brute(k, arr):
        if check_array(k, arr):
            return len(arr)
        best = 0
        for num in range(1, len(arr)):
            to_remove = list(combinations(arr, num))
            for option in to_remove:
                test_arr = list(arr)
                for el in option:
                    test_arr.remove(el)
                if check_array(k, test_arr) == True:
                    best = max(len(test_arr), best)
        return best
    
    def nonDivisibleSubset(k, arr):
        resid_cnt = [0] * k
        for el in arr:
            resid_cnt[el%k] += 1
        res = min(1, resid_cnt[0])
        for ind in range(1, (k//2)+1):
            if ind != k - ind:
                res += max(resid_cnt[ind], resid_cnt[k - ind])
        if k % 2 == 0 and resid_cnt[int(k/2)] != 0:
            res += 1
        return res
        
    if __name__ == "__main__":
        n, k = input().strip().split(' ')
        n, k = [int(n), int(k)]
        arr = list(map(int, input().strip().split(' ')))
        result = nonDivisibleSubset(k, arr)
        print(result)
    
  • + 1 comment

    Wordy but get the job done.

    public static int nonDivisibleSubset(int k, List<Integer> s) {
        Map<Integer, Integer> modToCount = new HashMap<>();
        for (Integer q : s) {
            int r = q % k;
            int c = modToCount.getOrDefault(r, 0); 
            c++;
            modToCount.put(r, c); 
        }
        if (modToCount.containsKey(0)) {
            modToCount.put(0, 1); 
        }   
        if (k % 2 == 0 && modToCount.containsKey(k / 2)) {
            modToCount.put(k / 2, 1); 
        }   
        Set<Integer> skip = new HashSet<>();
        int count = 0;
        for (Integer rem : modToCount.keySet()) {
            if (skip.contains(rem)) {
                continue;
            }   
            int bad = k - rem;
            int val = modToCount.get(rem);
            if (modToCount.containsKey(bad)) {
                if (modToCount.get(bad) > val) {
                    val = modToCount.get(bad);
                }   
                skip.add(bad);
            }   
            count += val;
        }   
        return count;
    }