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

Have something to add?
Draft saved Draft deleted

Similar Threads - Largest subset whose Date
C/++/# Largest permutation of elements in the range 1,...,N Aug 16, 2016
C/++/# Largest number formed by replacing k digits Aug 9, 2016
Inner Joins between Subsets of the Same Table. Dec 17, 2015
Largest number in f90 Jul 10, 2012
Algorithm: largest of the four Jan 11, 2011