The task is to return array with values which satisfy the condition: for each max the less or equal numbers count.
Example Input: numbers = [1, 2, 1, 4] maxes = [2, 4]. Output: [3, 4]
Explanation => 1, 2 and 1 are 3 numbers less than or equal to 2, and all 4 numbers satisfy the condition for 4.
Here is my naive approach code in C#, I have a feeling there can be an optimization, and would be thankful to see one:
static int[] counts(int[] nums, int[] maxes)
{
int queries = maxes.Length;
int numsLength = nums.Length;
int[] counts = new int[queries];
for (int i = 0; i < queries; i++)
{
for (int j = 0; j < numsLength; j++)
{
if (nums[j] <= maxes[i])
{
++counts[i];
}
}
}
return counts;
}
return maxes.Select(max => nums.Count(n => n <= max));
\$\endgroup\$nums
andmaxes
can contain? \$\endgroup\$