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

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Sum
AI Thread Summary
The discussion centers around a coding issue related to a solution that is failing test cases. The user initially believed their approach was straightforward but later realized a misunderstanding about the functionality of the "Skip" method in C#. The code attempts to find the maximum count of integers from a list that, when summed with any other integer, do not yield a result divisible by a specified integer k. The user acknowledges the confusion regarding the programming language, confirming it as C#. Additionally, there is a note about adjusting code tags for proper syntax highlighting in the forum. Ultimately, the user expresses relief at having resolved their issue.
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:
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top