Given an integer array and size of subarray, find the first subarray with leat average in single loop. Print first index of subarray and average. Problem is I can't use variable substring length c without using any additional loop. My code is-
public void FindFirstSub(int a[], int b, int c)
{
int average=0,sum=0,leastav=0;
for(int i=0;i<a.length-c;i++)
{
sum = 0;
sum = a[i]+a[i+1]+a[i+2]; // Here is problem.
System.out.print(a[i]+" "+a[i+1]+" "+a[i+2]+"\n");
average = sum/c;
if(leastav==0)
leastav=average;
else if(leastav>average)
leastav=average;
}
System.out.println("Least average is "+leastav);
}