Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. public static int KadaneWithIndices(int[] array){
  2. int currentMax = Integer.MIN_VALUE;
  3. int totalMax = Integer.MIN_VALUE;
  4. int startIndex = 0, endIndex = 0, tempIndex = 0;
  5.  
  6. for(int i = 0; i < array.length; i++){
  7. currentMax += array[i];
  8.  
  9. if(currentMax < 0){
  10. currentMax = 0;
  11. tempIndex = i + 1;
  12. }
  13. else if(totalMax < currentMax){
  14. totalMax = currentMax;
  15. startIndex = tempIndex;
  16. endIndex = i;
  17.  
  18. }
  19. }
  20. System.out.println("Start index: " + startIndex + " End index: " + endIndex);
  21. return totalMax;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement