Answer the question
In order to leave comments, you need to log in
How to handle System.OverflowException?
I started doing tasks on the Codewars platform and ran into a problem that I solved the task, the tests that exist, the program passes, and on the site the program gives the error System.OverflowException : Array dimensions exceeded supported range. Since I don’t know what else they have for tests, I tried to increase the number that goes into the function, but I can’t fix the error, I think for a long time, so I ask you to help.
namespace Test
{
using System;
public static class Program
{
public static ulong OddCount(ulong n)
{
ulong count = 0, odd = 0;
ulong[] array = new ulong[n];
for (ulong i = 0; i < n; i++)
{
count++;
array[i] = count;
if (array[i] % 2 != 0 && array[i] > 0)
{
odd++;
}
}
Console.WriteLine(odd);
return odd;
}
static void Main(string[] args)
{
ulong i = 25000;
OddCount(i);
Console.ReadKey();
}
}
}
//NUnit Test
namespace Solution
{
using NUnit.Framework;
using System;
[TestFixture]
public class SampleTest
{
[Test]
public void Test()
{
Assert.AreEqual(7, Kata.OddCount(15));
Assert.AreEqual(7511, Kata.OddCount(15023));
}
}
}
Answer the question
In order to leave comments, you need to log in
Obviously, in the tests, a number is shoved there, such that it is impossible to allocate an array of this size. I will assume that this is something like more than 2 ^ 32.
To solve this problem, an array is not needed at all.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question