import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    
    public static int maxSumforlenk(int [] arr , int k ){
        
        int n = arr.length;
        int s = 0 ;
        int e = k-1 ; 
        int maxSum = Integer.MIN_VALUE;
        
        while ( e < n ){
            int sum = 0 ;
        
            for(int i= s ; i <= e ; i++){
                sum+= arr[i];
            }
            
            maxSum = Math.max(maxSum , sum);            
            s++;
            e++;
        }
        
        return maxSum;
    }

    
    public static void main(String[] args) {
        int [] arr = {2,3,9,-1,7,1,0};
        int k = 4 ;
        System.out.println("MAXIMUM SUM WITH Length : 4 = " + maxSumforlenk(arr,k));
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: