This blog site is dedicated for articles on problem solving using C, C++, data structures, algorithms. Besides this few technical articles are also presented.
Mar 7, 2022
Find maximum average subarray of k length
#include
using namespace std;
int maxAverageSubarray(int a[], int alen, int k)
{
if(alen < k)
return -1;
int max_sum = 0;
for(int i=0;i max_sum)
{
max_sum = sum;
index = i;
}
}
return index-k+1;
}
int main() {
// your code goes here
int arr[] = {1, 12, -5, -6, 50, 3};
int k = 4;
int n = sizeof(arr)/sizeof(arr[0]);
cout << "The maximum average subarray of "
"length "<< k << " begins at index "
<< maxAverageSubarray(arr, n, k);
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment