fork(10) 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) {
  11. int [] input = {1, 0, 0, 1, 0, 0, 1,1,1,1, 0,1};
  12. System.out.println(findMaxNumberOfOnes(input,0, input.length-1));
  13. }
  14.  
  15. private static int findMaxNumberOfOnes(int[] input, int i, int j) {
  16. if (i==j)
  17. return 1;
  18. int option1 = input[i] + findMaxNumberOfOnes(input, i+1, j);
  19. int option2 = count(input , i , j, true);
  20. int option3 = count(input, i, j, false);
  21. int option4 =findMaxNumberOfOnes(input, i, j-1) +input[j];
  22. return Math.max(option1, Math.max(option2,Math.max(option3,option4)));
  23. }
  24.  
  25. private static int count(int[] input, int i, int j, boolean flipped) {
  26. int a = flipped?0:1;
  27. int count = 0;
  28. while (i<=j){
  29. count += (input[i++]==a)?1:0;
  30. }
  31. return count;
  32. }
  33. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
10