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

// The main method must be in a class named "Main".
class Main {
    
    public static long maxSumforlenk(int [] arr , int k ){
        int n = arr.length;

        //First Window
        long sum = 0;
        for(int i=0; i< k ; i++){
            sum += arr[i];
        }

        //Second Window
        int s=1;
        int e = k;
        long maxSum = sum;
        while(e<n){
            sum = sum + arr[e] - arr[s-1];
            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: