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 think your solution is OK , but to work properly it should be something like :
a[n]//the array
minimum[n-l+1]//fixed
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]) //fixed
minpos=i+l-1; //fixed
minimum[i] = a[minpos];
}
// Complexity Analysis :
//Time - O(n^2) in worse case(array is sorted) we will run
"position_minimum_in_subarray" on each iteration
//Space - O(1) - "minimum array" is required for store the result
If you want to improve your time complexity, you can do it with additional space. For example you can store each sub array in some self-balancing BST (e.g. red-black tree) and fetch minimum on each iteration :
for (int i= 0; i