Maximum average subarray | Practice | GeeksforGeeks

Given an array Arr of size N and a positive integer K, find the sub-array of length K with the maximum average. 

Example 1:
Input:
K = 4, N = 6
Arr[] = {1, 12, -5, -6, 50, 3}
Output: 12 -5 -6 50
Explanation: Maximum average is 
(12 - 5 - 6 + 50)/4 = 51/4.

Example 2:
Input:
K = 3, N = 7
Arr[] = {3, -435, 335, 10, -50, 100, 20}
Output: 335 10 -50
Explanation: Maximum average is 
(335 + 10 - 50)/3 = 295/3.


Your Task:
You don't need to read input or print anything. Your task is to complete the function findMaxAverage() which takes the array of integers arr, its size n and integer k as input parameters and returns an integer denoting the starting index of the subarray.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints
1 <= N <= 105
0 <= |Arr[i]| <= 103
0
avinashdhn19041 week ago
 // simple solution using sliding window
  
  
  int findMaxAverage(int arr[], int n, int k) {
    int sum=0;
        for(int i=0;i<k;i++)sum+=arr[i];
       int mx=sum;
       pair<int,int>p={0,k-1};
        for(int i=0;i<n-k;i++){
            sum-=arr[i];
            sum+=arr[k+i];
            if(sum>mx){
                mx=sum;
                p={i+1,k+i};
            }
        }
    return p.first;
    }
+1
dronzerdracel3 weeks ago
Sliding window|C++
int i=0,j=0,sum=0,maxm=INT_MIN,pos=-1;
        while(j<n){
            sum+=arr[j];
            if(j-i+1==k){
                if(sum>maxm){
                    maxm=sum;
                    pos=i;
                }
                sum-=arr[i];
                i++;
            }
            j++;
        }
        return pos;
0
srisai22601 month ago
PYTHON SOLUTION:
class Solution:   def findMaxAverage(self, arr, n, k):       sum=0       idx=0       maxsum=0       for i in range(k):           sum+=arr[i]       maxsum=sum                  for j in range(k,n):               sum+=arr[j]-arr[j-k]               if sum > maxsum:                   maxsum = sum                   idx = j-k+1       return idx
0
vainalavinayvvk0981 month ago
class Solution:   def findMaxAverage(self, arr, n, k):       # code here       maxsum = -99999999999999       index = 0       first = 0       summ = sum(arr[:k])       if summ>maxsum:           summ = maxsum           index = k-1       for i in range(k,n):           summ+=arr[i]           summ-=arr[first]           first += 1           if summ >maxsum:               maxsum = summ               index = i           #print(summ,index)       return index-k+1 
+1
bahubuli1 month ago
int findMaxAverage(int arr[], int n, int k) {
      
      double ans =INT_MIN,sum=0;
      int idx,j=0;
      
      for(int i=0;i<n;i++)
      {
         if(i<k-1) sum+=arr[i];
         
         else
         {
             sum+=arr[i];
             
             if((sum/k)>ans)
             {
                 idx = i+1-k;
                 ans = sum/k;
             }
             sum-=arr[j];
             j++;
             
         }
          
      }
      return idx;
    }
+4
subha_nik1 month ago
C++ Solution:
Sliding window technique
Code:
class Solution{   
public:
    int findMaxAverage(int arr[], int n, int k) {
        int idx = 0, sum = 0, maxsum = 0;
        for(int i=0; i<k; i++)
            sum += arr[i];
        maxsum = sum;
        
        for(int i=k; i<n; i++) {
            sum += arr[i]-arr[i-k];
            if(sum > maxsum) {
                maxsum = sum;
                idx = i-k+1;
            }
        }
        
        return idx;
    }
};
0
strsatyam1 month ago
//using sliding window
int findMaxAverage(int arr[], int n, int k) {
       if(n<k)
            return 0;
       int t =0;
       for(int i=0;i<k;i++){
           t+=arr[i];
       }
       int ans=t;
       int idx=0;
       for(int j=k;j<n;j++){
           t+=arr[j];
           t-=arr[j-k];
           if(t>=ans){
               ans=t;
               idx=j-k+1;
           }
       }
       return idx;
    }
0
akashmr10961 month ago
 int findMaxAverage(int[] arr, int n, int k) {
        int sum = 0;
        for (int i=0; i<k; i++) {
            sum += arr[i];
        }
        
        int maxSum = sum;
        int index = 0;
        for (int i=k; i<n; i++) {
            sum -= arr[i-k];
            sum += arr[i];
            if (sum > maxSum) {
                index = i-k+1;
                maxSum = sum;
            }
        }
        
        return index;
    }
+1
triple2double11 month ago
NOOB SOLUTION
int findMaxAverage(int arr[], int n, int k) {
       if(n<k)
            return 0;
       int sum =0;
       for(int i=0;i<k;i++){
           sum+=arr[i];
       }
       int ans=sum;
       int idx=0;
       for(int j=k;j<n;j++){
           sum+=arr[j]-arr[j-k];
           if(sum>=ans){
               ans=sum;
               idx=j-k+1;
           }
       }
       return idx;
    }
+3
tandava12341 month ago
//SIMPLE C++ CODE
int findMaxAverage(int arr[], int n, int k) {       // code here       vector<int> v;        int sum=0;       for(int i=0;i<k;i++)       {           sum+=arr[i];       }       int inx=0,val=sum;       for(int i=k;i<n;i++)       {           sum=sum+arr[i]-arr[i-k];           if(val<sum)           {               val=sum;               inx=i-k+1;           }       }       return inx;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.