Given an array and two more arrays i need to find the range of elements in the first array
For e.g. MainArray={2,4,6,5,8,9}, range1={4,5,6}, range2={6,9,8}
for First-Iteration i have to select elements in MainArray in range [4,6] ->[4,6,5] --[3] is the output
for second-Iteration i have to select elements in MainArray in range [5,9] ->[5,8,9]--[3] is the output
for third-Iteration i have to select elements in MainArray in range [6,8] ->[6,8]--[2] is the output
array returned [3,3,2]
static void Main(string[] args)
{
var rng = new Random();
var result = processFunc(Enumerable.Range(0, 5000000).OrderBy(x => rng.Next()).ToArray(),
Enumerable.Range(0, 20000).OrderBy(x => rng.Next()).Take(200).ToArray(),
Enumerable.Range(0, 20000).OrderBy(x => rng.Next()).Take(200).ToArray());
}
public static int[] processFunc(int[] scores,int[] l,int[] r)
{
IList<int> output = new List<int>();
for (int i = 0; i < l.Length; i++)
{
var bestMatch = scores.Where(x => x >= l[i] && x <= r[i]);
output.Add(bestMatch.Count());
}
return output.ToArray();
}
The code runs fine when numbers are small but once they >50,000 the program becomes slow. How can I optimize this solution ?