coderrohan14

Solution4Java

Sep 30th, 2021 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution{
  4. public static void main(String[] args){
  5. Scanner sc = new Scanner(System.in);
  6. int t = sc.nextInt();
  7. StringBuilder ans = new StringBuilder("");
  8. while (t-- > 0) {
  9. int n = sc.nextInt();
  10. int[] arr = new int[n];
  11. for(int i=0;i<n;i++){
  12. arr[i] = sc.nextInt();
  13. }
  14. long max_so_far = Long.MIN_VALUE, max_ending_here = 0;
  15. for(int i=0;i<n;i++){
  16. max_ending_here = max_ending_here + arr[i];
  17. max_so_far = Math.max(max_so_far,max_ending_here);
  18. if (max_ending_here < 0)
  19. max_ending_here = 0;
  20. }
  21. ans.append(max_so_far+"\n");
  22. }
  23. System.out.println(ans);
  24. }
  25. }
Add Comment
Please, Sign In to add comment