dsa Maximum-Subarray

package com.company;
import java.util.*;

public class Main {

public static void main(String[] args) {
// sum of subarray
Scanner sc = new Scanner(System.in);
int a[]={-2,-3,4,-1,-2,1,5,-3};
int sum=0;
int max=a[0];
for(int i=0;i<a.length;i++) {
sum+=a[i];
if (sum > max) {
max = sum;
} else if (sum < 0) {
max = max;
sum = 0;
}
}

System.out.println(max);



}





}


Comments

Popular posts from this blog

Discover the Best Steel Manufacturer: Adnan & Sons

Online Cabinets and Lockers in Pakistan: Discover Adnan & Sons' Top-Quality Steel Products

The Best Emergency Exit Door in Pakistan by Adnan & Sons