Running through exercises on testdome...currently looking at https://www.testdome.com/for-developers/solve-question/9877
Implement function
CountNumbers
that accepts a sorted array of integers and counts the number of array elements that are less than the parameterlessThan
.For example,
SortedSearch.CountNumbers(new int[] { 1, 3, 5, 7 }, 4)
should return 2 because there are two array elements less than 4.
I have entered:
public class SortedSearch
{
public static int CountNumbers(int[] sortedArray, int lessThan)
{
int returnedValued = 0;
foreach (int i in sortedArray)
{
if(i<lessThan)
returnedValued += 1;
}
return returnedValued;
}
public static void Main(string[] args)
{
Console.WriteLine(SortedSearch.CountNumbers(new int[] { 1, 3, 5, 7 }, 4));
}
}
I was wondering why this was marked as difficulty level hard and expected time 20 min when I knew it should only take a few. Anyway, 2 out of 4 cases passed. I'm failing on time limit exceeded and I'm guessing I need to refactor to return a faster search. Is this right? And if so could anyone help with it?
Example case: Correct answer Various small arrays: Correct answer Performance test when sortedArray contains lessThan: Time limit exceeded Performance test when sortedArray doesn't contain lessThan: Time limit exceededSee Question&Answers more detail:os