1

This code is supposed to take 2 lines of input

  1. Number of integers k
  2. k integers seperated by a space

This code works fine for inputs of less than 230 integers but it's not working for more. I mean for inputs like 1000s and more. What is the issue?

I think the whole line is not read from console. But how to read the whole line?

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MyClass
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(Console.OpenStandardInput());

        // Reads the number of integers to input
        int k = int.Parse(reader.ReadLine());

        List<int> pokemons = new List<int>(k);

        string inputLine;

        List<string> input = new List<string>(k);
        int a;

        inputLine = reader.ReadToEnd();

        input.AddRange(inputLine.Split(new char[] { ' ' }));

        Console.WriteLine(input.Count);

// Size of the input is never greater than 230.

Yu Zhang
  • 2,037
  • 6
  • 28
  • 42
Ankush
  • 173
  • 2
  • 14
  • 2
    http://stackoverflow.com/questions/5557889/console-readline-max-length – Tim Freese Oct 21 '15 at 19:57
  • 1
    You are initialising a list as it would be an array. The distinctive feature of lists is precisely that you don't need to define their dimensions. The right version: `List pokemons = new List();` (same thing with the other one which triggers the error) `k` is redundant (or use it to declare an array: `int[] pokemons = new int[k];`). By the way, you should put your `StreamReader` inside `using` (or dispose it properly at the end). – varocarbas Oct 21 '15 at 20:05
  • Also note that there is an overload of `Split` which is better for this situation (just one character): `inputLine.Split(' ')`. – varocarbas Oct 21 '15 at 20:08
  • Thanks for replying so fast @varocarbas But the problem i am facing is that the Console.ReadLine() is reading only a part of the string – Ankush Oct 21 '15 at 20:14
  • 1
    @varocarbas doing `new List(k)` is not redundant, if you know how many entries you will be reading ahead of time you can optimize your program by feeding that number in to the constructor of `List`, it will pre-allocate that much space in the internal buffer and fewer resizes will need to happen as you are adding members. Now doing `input = new List(k);` then doing `input.AddRange(inputLine.Split(...` I could consider redundant, you could instead directly do `input = new List(inputLine.Split(...` directly. – Scott Chamberlain Oct 21 '15 at 20:14
  • i was working on a challenge on www.hackerearth.com. They would pass 2100 integers to this program and check the output. But i don't know why the input is not being read completely – Ankush Oct 21 '15 at 20:18
  • @ScottChamberlain Thanks for the clarification. Sorry if I wasn't clear. It is redundant in this context, for the OP. In fact, this is precisely what is provoking his error. – varocarbas Oct 21 '15 at 20:18
  • Congratulations but this is not relevant (or yes, to be off-topic here perhaps). I recognised the structure of the inputs (HackerRank, Google Code Jam...) and that's why I gave you the solution you needed. Read Scott's comment above to understand why you are having this problem; then implement the corrections I have suggested and everything would work fine. – varocarbas Oct 21 '15 at 20:20
  • @varocarbas After all these corrections i am still getting only 230 integers. The problem is with the reader.ReadToEnd() method -> it is returning a string having char_length of 1018, which is way less than the input – Ankush Oct 21 '15 at 20:29
  • @ScottChamberlain thanks for the tips. Those were very silly mistakes. But the problem is still nowhere to be solved – Ankush Oct 21 '15 at 20:30
  • Yes. Your codes is workse than what I thought after a quick look. You don't even know what you are doing. I will do a summary (even without reading the problem): `k` is the number of lines; each lines has `x` elements (separated by blank spaces), you are putting all the lines together in one string and then splitting this string by blank spaces. That is: if you inputs are line 1: a b; line 2: c d; you are doing a b c d, then split and get 4 elements, when k 2. If you would treat the list as a list no error would be triggered, but your inputs would be crazily wrong? – varocarbas Oct 21 '15 at 20:31
  • @varocarbas yes you got it right. – Ankush Oct 21 '15 at 20:32
  • @varocarbas Each line has k elements (seperated by blank spaces). I just have to put them in an array – Ankush Oct 21 '15 at 20:34

0 Answers0