Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Aug 3, 2016 #1
    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 (C):

    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))
                if(ss.Count > max)
                    max = ss.Count;
    Last edited by a moderator: Aug 3, 2016
  2. jcsd
  3. Aug 3, 2016 #2
    Ah, nevermind. Skip doesn't do what I thought it does.
  4. Aug 3, 2016 #3


    Staff: Mentor

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted