fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13.  
  14. int [] ar = {1, -3, 2, 1, -1};
  15. int max = 0;
  16. int max_i = -1;
  17. int max_j = -1;
  18. for(int i=0;i< ar.length;i++){
  19. int sum = ar[i];
  20. for(int j=i+1;j<ar.length;j++){
  21. sum += ar[j];
  22. if( sum > max ){
  23. max_i = i;
  24. max_j = j;
  25. max = sum;
  26. }
  27. }
  28. System.out.println("max here ="+max);
  29. }
  30.  
  31. System.out.println(max+ " "+max_i+ " "+max_j);
  32. }
  33. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
max here =1
max here =1
max here =3
max here =3
max here =3
3  2  3