I faced this issue (runtime error)in several contests.
Every time I try to solve problems using C# language it throws me a runtime error.
Let’s take example:
Today I tried to solve easy program from Overnite Coding contest » Transitions.
Here is the solution:
using System;
using System.Linq;
namespace CodeOverflow_1._1
{
public class TRNSN
{
public static void Main(string[] args)
{
int length = Convert.ToInt32(Console.ReadLine());
var result = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse).Distinct().Count() - 1;
Console.WriteLine(result);
Console.ReadLine();
}
}
}
or without any linq operations
using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
int length = Convert.ToInt32(Console.ReadLine());
var inputs = Console.ReadLine().Split(' ');
List<long> list = new List<long>();
long count = 0;
foreach (var input in inputs)
list.Add(long.Parse(input));
list.Sort();
for (int i = 0; i < length - 1; i++)
if (list[i] != list[i + 1])
count++;
Console.WriteLine(count);
}
}
Other solutions:
When I check others solution, they used the same logic but with a different language. Why this code is not working? the logic seems correct to me.
Any help would really appreciated.