The minimum number in a subarray of size L. I have to find it for all the subarrays of the array. Is there any other way than scanning through all the subarrays individually?
I have one solution in mind:
a[n]//the array
minimum[n-l+1]//the array to store the minimum numbers
minpos=position_minimum_in_subarray(a,0,l-1);
minimum[0]=a[minpos];
for(i=1;i<=n-l-1;i++)
{
if(minpos=i-1)
{
minpos=position_minimum_in_subarray(a,i,i+l-1);
}
else {
if(a[minpos]>a[i+l-1]) minpos=i+l-1;
minimum=a[minpos];
}
}
Is there any better solution than this?