Given an array of elements, find the maximum possible sum of a Contiguous subarray Non-contiguous (not necessarily contiguous) subarray.
import java.io.*;
import java.util.*;
public class Solution {
int size;
int[] arr;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n, i ;
n = sc.nextInt();
Solution[] listOfObjs = new Solution[n];
for(i=0;i max)
max = ele;
}
System.out.print(max);
}
else
{
int sumSoFar = Integer.MIN_VALUE, sumEnding =0;
for(int i=0; i < s.arr.length ; i++)
{
sumEnding = sumEnding + s.arr[i];
if(sumEnding < 0)
sumEnding = 0;
if(sumEnding > sumSoFar)
sumSoFar = sumEnding;
}
System.out.print(sumSoFar);
}
}
public static void printUnContigiousSum(Solution s)
{
int sum = 0;
boolean allElementNeg = areAllEleNeg(s.arr);
if(allElementNeg)
{
int max = Integer.MIN_VALUE;
for(int ele : s.arr)
{
if(ele > max)
max = ele;
}
System.out.print(max);
}
else
{
for(int ele : s.arr)
{
if(ele > 0)
sum = sum + ele;
}
System.out.print(sum);
}
}
public static boolean areAllEleNeg(int[] arrr)
{
boolean isNeg = true;
for(int ele : arrr)
{
if(ele > 0)
{
isNeg = false;
break;
}
}
return isNeg;
}
}