Skip to content

Instantly share code, notes, and snippets.

@Ram-1234
Created June 29, 2021 17:08
Show Gist options
  • Save Ram-1234/beb5a1f6cbe190cafdac157753bd9320 to your computer and use it in GitHub Desktop.
Save Ram-1234/beb5a1f6cbe190cafdac157753bd9320 to your computer and use it in GitHub Desktop.
Maximum Sub Array Problem
/* 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