0

When inside for loop, code doesnt work. Error: Index was outside the bounds of the array. When i put code in main method and manually try, everything works. Can u tell me whats wrong?

namespace HackerRankProblems
{
    class Program
    {

        static int[] serviceLane(int n, int[] width, int[,] cases)
        {
            List<int> list = width.OfType<int>().ToList();
            List<int> returnList = new List<int>();
            List<int> tempList;
            for (int i = 0; i < n; i++)
            {
                tempList = list.GetRange(cases[i, 0], cases[i, 1]);
                returnList.Add(tempList.Min());



            }
            int[] returnArray = returnList.ToArray();
            return returnArray;

        }


        static void Main(string[] args)
        {

            int[,] cases = new int[,]
            {
                {1,2},
                {3,4},
                {5,7}
            };

            int[] width = new int[] { 2, 3, 1, 2, 3, 2, 3, 3 };

            List<int> lista = width.OfType<int>().ToList();
            List<int> returnLista = new List<int>();

            List<int> tempList = lista.GetRange(cases[3, 0], cases[1, 1]);
            returnLista.Add(tempList.Min());
            int[] returnArray = returnLista.ToArray();
            string.Join(",", serviceLane(3, width, cases));// Error


        }
    }
}
Frank Nielsen
  • 1,546
  • 1
  • 10
  • 17
  • 1
    Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Progman Apr 20 '20 at 20:01
  • 1
    hint - `cases[3,0]` – phuzi Apr 20 '20 at 20:03
  • Not enough cases – Tomas Paul Apr 20 '20 at 20:06
  • @phuzi on your hint, read the question, yes thats a mistake in main method, what is the mistake in serviceLane method. Like i said code in main method works, by a mistake i put 3,0. – Marko Rakic Apr 20 '20 at 20:13

3 Answers3

0

The problem is list.GetRange(cases[i, 0], cases[i, 1]). When n is 2, then the call to list.GetRange(cases[i, 0], cases[i, 1]) is the same as list.GetRange(5, 7) but width/list only has 8 elements, not 12!

Without knowing exactly what you want to achieve/what the expected output should be, it's difficult to know what it should be!

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • This is hackerrank problem. In simple terms i have to get specific range from an arrray, in this case list. And then search for a min number in that range. Then return it in new array. Sorry for grammatical errors, english is not my native language. – Marko Rakic Apr 20 '20 at 21:08
  • Thought the point of HackerRank was that you don't use sites like StackOverflow! – phuzi Apr 21 '20 at 09:28
0

Your problem is happening when n = 2 and it's happening because cases[i, 0] which is 5 and cases[i, 1] which is 7 sum up to 12 when your listlength is 8, I think what you are trying to do is this

static int[] serviceLane(int n, int[] width, int[,] cases)
{
    List<int> list = width.OfType<int>().ToList();
    List<int> returnList = new List<int>();
    List<int> tempList;
    for (int i = 0; i < n; i++)
    {
        tempList = list.GetRange(cases[i, 0], cases[i, 1] - cases[i, 0]);
        returnList.Add(tempList.Min());
    }
    int[] returnArray = returnList.ToArray();
    return returnArray;

}
SyBom
  • 96
  • 9
  • This is hackerrank problem. In simple terms i have to get specific range from an arrray, in this case list. And then search for a min number in that range. Then return it in new array. Sorry for grammatical errors, english is not my native language. – Marko Rakic Apr 20 '20 at 21:18
  • Thought the point of HackerRank was that you don't use sites like StackOverflow! – phuzi Apr 21 '20 at 09:44
0

Way too many arrays and lists. All those conversions back and forth effectively make complete copies of the data. You can save a lot of work by sticking with IEnumerable.

class Program
{

    static IEnumerable<int> serviceLane(int[] width, int[,] cases)
    {
        return Enumerable.Range(0, cases.GetUpperBound(0)).
               Select(i => width.Skip(cases[i,0]).Take(cases[i,1]).Min());
    }

    static void Main(string[] args)
    {
        int[,] cases = new int[,]
        {
            {1,2},
            {3,4},
            {5,7}
        };

        int[] width = new int[] { 2, 3, 1, 2, 3, 2, 3, 3 };

        //changed to [1,0] to say within the bounds of the array
        var temp = width.Skip(cases[1,0]).Take(cases[1,1]);
        var resultArray = new int[] {temp.Min()};
        var resultString = string.Join(",", serviceLane(width, cases));
    }
}

However, this program will have the same problem as the original: bad data. The width array is not large for all of the possible sample cases. We can adjust the method to account for this:

static IEnumerable<int> serviceLane(int[] width, int[,] cases)
{
    return Enumerable.Range(0, cases.GetUpperBound(0)).
         Select(i => 
         {
             if (cases[i,0] + cases[i,1]) >= width.Length)
             {
                 //do something here. Width is not long enough.
             }
             else 
                 return width.Skip(cases[i,0]).Take(cases[i,1]).Min();
         });
}

But probably we're not seeing the width sample data correctly.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794