I have a very long array numbers[]. My algorithm needs to find the lowest index j in numbers[] at which the |numbers[j] - numbers[i]| <= x(a random variable) or where |numbers[j] - numbers[i]| >= m - x (m another variable, larger than x) and where i<j.
This is my algorithm now:
for (int j = 1; j < numbers.Length; j++)
{
for (int i = 0; i < j; i++)
{
long diff = Math.Abs(numbers[j] - numbers[i]);
if (diff <= x || diff >= m - x)
return j;
}
}
Can this be done more efficiently? An input with very high numbers, for instance, takes my laptop about 36 seconds.
|| diff >= m - xx, other than the implied limit ofInt32.MaxValue?jindex? Why not returningias well?