DEV Community

Vasanth S
Vasanth S

Posted on

1

Practice Problem in Java day 9

Today I covered the sliding window problem to calculate the maximum sum of sub array using kadane's algorithm in java

public class sumofsubarr {
static int max(int a, int b){
return a>b? a : b;
}
public static void main(String[] args) {
int[] arr={1,3,5,2,4,6};
int k=3;
int maxS=0;
int curS=0;
if (k> arr.length) System.out.println(-1);
for (int i=0;i<k;i++) {
curS += arr[i];
}
maxS=curS;
for (int i=k;i< arr.length;i++){//kadanes algorithm
curS=curS+arr[i]-arr[i-k];
maxS=max(maxS,curS);
}

    System.out.println(maxS);
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post