Find Profit:
public class Solution{ public int findMaxProfit(int[] arr){ int leftLeastPrice = arr[0]; int maxProfit = 0; int profit = 0; for(int i =1; i<arr.length; i++){ profit = arr[i] - leftLeastPrice; if(profit > maxProfit){ maxProfit = profit; }else if(profit < 0){ leftLeastPrice = arr[i]; } } return maxProfit; }}
4.1.1
Will return the subarray whose value of sum*-1 is smallest.
4.1.4
If the sum of subarray is negative, return 0
4.1.5
public class Solution{ public int findMaxSub(int[] arr){ int sum = 0; int sumMax = 0; for(int i =0 ; i <arr.length; i++){ sum += arr[i]; if(sum > sumMax){ sumMax = sum; }else if(sum < 0){ sum = 0; } } return sumMax; }}