Largest subset whose every pair's sum doesn't divide K

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Sum
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 1K views
SlurrerOfSpeech
Messages
141
Reaction score
11
Any idea where I'm going wrong here? It's failing some test cases. I thought my solution was straightforward (if not brute force).

Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{
    static void Main(String[] args)
    {
        int k = Int32.Parse(Console.ReadLine().Split(' ')[1]);
        var S = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
      
        int max = Int32.MinValue;
        foreach(int i in S)
        {
            var ss = new List<int>() { i };
            foreach(int j in S.Skip(i))
                if(ss.All(m => (m + j) % k != 0))
                    ss.Add(j);
            if(ss.Count > max)
                max = ss.Count;
        }
          
        Console.WriteLine(max);
    }
}
 
Last edited by a moderator:
Physics news on Phys.org
Ah, nevermind. Skip doesn't do what I thought it does.
 
Ok, I couldn't decide what programming language you were using, I assumed it was C#.

Anyway I adjusted your code tags to turn on syntax highlighting ie CODE=JAVA or CODE=PYTHON ... C# and C++ revert to C.

Glad you found your answer.