Created
June 29, 2021 17:08
-
-
Save Ram-1234/beb5a1f6cbe190cafdac157753bd9320 to your computer and use it in GitHub Desktop.
Maximum Sub Array Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* package whatever; // don't place package name! */ | |
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
/* Name of the class has to be "Main" only if the class is public. */ | |
class Ideone | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
Scanner sc=new Scanner(System.in); | |
int n=sc.nextInt(); | |
int[] a=new int[n]; | |
int max=0; | |
for(int i=0;i<n;i++){ | |
a[i]=sc.nextInt(); | |
} | |
for(int i=1;i<n;i++){ | |
a[i]+=a[i-1]>0 ? a[i-1]:0; | |
max=Math.max(max,a[i]); | |
} | |
for(int i=0;i<n;i++){ | |
System.out.print(a[i]+" "); | |
} | |
System.out.println(); | |
System.out.println(max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment