Advertisement
Guest User

Max_sub2

a guest
Apr 9th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. package week1;
  2.  
  3. /*
  4.  * @author Tanzim Ibn Patowary
  5.  * Created  Apr 9, 2020 3:50:54 PM
  6.  */
  7. public class MaxSum {
  8.     public static int maxSubArray(int[] nums) {
  9.         int startpoint = nums[0];
  10.         int storesum = 0;
  11.         for (int i = 0; i < nums.length; i++) {
  12.             storesum = storesum + nums[i];
  13.             if (startpoint < storesum) {
  14.                 startpoint = storesum;
  15.             }
  16.             if (storesum < 0) {
  17.                 storesum = 0;
  18.             }
  19.         }
  20.         return storesum;
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.         // TODO Auto-generated method stub
  25.         int myArray[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
  26.         System.out.println(maxSubArray(myArray));
  27.     }
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement