Sort by

recency

|

790 Discussions

|

  • + 0 comments

    how is it possible to be a set of "Distinct" numbers and at the same time having two tens? what is the meaning of distinct here?

  • + 0 comments

    My versiion:

        s=[x%k for x in s]
        s1=[x for x in s if x!=0]
        if 0 in s:
            s1.append(0)
        counter=[0] * (k+1)
        for x in s1: 
            counter[x]+=1
        count=0;
        for i in range(0,k//2+1):
            max_k=max(counter)
            max_ind_rem=counter.index(max_k)
            if max_ind_rem*2==k:
                if max_ind_rem *2== k :
                    count=count+1
                else:
                    count=count+max(1, counter[k-max_ind_rem])
            else:
                count+=max_k
           #we include already
            counter[max_ind_rem]=0;
            counter[k-max_ind_rem]=0
        return count 
    
  • + 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; 
    }
    
  • + 0 comments

    Fun puzzle! Here's a C# solution: ` static class NonDivisibleSubsetResult { private class MultiSetModK { private readonly Dictionary Counts; private readonly List distinct; private readonly Dictionary> PartnerDict;

        public MultiSetModK(List<int> source, int k)
        {
            List<int> modded = source.Select(x => x % k).ToList();
            Console.WriteLine("\tAfter modding: " + string.Join(',', modded));
            distinct = modded.Distinct().ToList();
            Counts = distinct.ToDictionary(
                a => a,
                a => modded.Count(b => b == a));
    
            PartnerDict =
            (from a in distinct
             let partners = new HashSet<int>(
                 from b in distinct
                 where (a + b) % k == 0
                 select b)
             select new { a, partners })
            .ToDictionary(kvp => kvp.a, kvp => kvp.partners);
        }
    
        public int? MostPartneredElement()
        {
            var result = (from x in distinct
                          let partnerCount = (
                              from y in PartnerDict[x]
                              select y == x ? Counts[y] - 1 : Counts[y]) // Don't count an int as its own partner
                              .Sum()
                          where partnerCount > 0
                          select (x, partnerCount))
                   .DefaultIfEmpty((-1, -1))
                   .MaxBy(t => t.Item2);
    
            if (result.Item2 >= 0)
                Console.WriteLine($"{result.Item1} has {result.Item2} partners: {string.Join(',', PartnerDict[result.Item1])}");
            return result.Item2 < 0 ? (int?)null : result.Item1;
        }
    
        public void RemoveOneOf(int? x)
        {
            if (x == null) return;
            Console.WriteLine("Removing " + x.Value);
    
            Counts[x.Value] = Counts[x.Value] - 1;
    
            if (Counts[x.Value] == 0)
            {
                // If none left, purge from the other structures
                distinct.Remove(x.Value);
    
                foreach (var kvp in PartnerDict)
                {
                    if (kvp.Value.Contains(x.Value))
                        kvp.Value.Remove(x.Value);
                }
            }
    
        }
    
        public int Count() => Counts.Values.Sum();
    
    }
    
    public static int nonDivisibleSubset(int k, List<int> s)
    {
        MultiSetModK modded = new(s, k);
    
        // Iterate as long as there is an in in the modded list with partners
        for (int? x = modded.MostPartneredElement(); x != null; x = x = modded.MostPartneredElement())
        {
            modded.RemoveOneOf(x);
        }
        return modded.Count();
    
    }
    

    } `

  • + 0 comments

    I've been diving into the Non-Divisible Subset problem recently, and it's such a fascinating challenge! Finding that perfect subset with the right mod properties is like solving a fun puzzle. By the way, if you’re ever in Abu Dhabi and need some car TLC, check out Kia Service Abu Dhabi - Mussafah | Specialist Repair Workshop. They do top-notch work! Here’s the https://alzaabiautocare.com/brands/kia/ if you want to learn more about their services.