metalni

maxPosSubSum

Nov 26th, 2020 (edited)
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class maxPosSubSum{
  4.     public static void main(String [] args){
  5.         int N;
  6.         Scanner in = new Scanner(System.in);
  7.         N = in.nextInt();
  8.         int numbers [] = new int[N];
  9.         for(int i=0; i<N; i++)
  10.             numbers[i] = in.nextInt();
  11.  
  12.         int sum, max = numbers[0];
  13.         for(int i=0; i<N; i++){
  14.             sum = 0;
  15.             for(int j=i; j<N; j++){
  16.                 sum += numbers[j];
  17.                 if(sum > max)
  18.                     max = sum;
  19.             }
  20.         }
  21.         System.out.println(max);
  22.     }
  23. }
Add Comment
Please, Sign In to add comment