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
}
}
}