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))
                        ss.Add(j);
                if(ss.Count > max)
                    max = ss.Count;
            }
             
            Console.WriteLine(max);
        }
    }
     
     
    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

    jedishrfu

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Largest subset whose every pair's sum doesn't divide K
  1. Largest number in f90 (Replies: 13)

Loading...