fork(1) 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.  
  11. public static int maxGap(int n) {
  12. // get rid of right-hand zeros
  13. while (n != 0 && (n & 1) == 0) {
  14. n >>>= 1;
  15. }
  16.  
  17. int max = 0;
  18. int gap = 0;
  19. while (n != 0) {
  20. if ((n & 1) == 0) {
  21. gap++;
  22. max = Math.max(gap, max);
  23. } else {
  24. gap = 0;
  25. }
  26. n >>>= 1;
  27. }
  28. return max;
  29. }
  30.  
  31. public static int maxGapX(int n) {
  32. if (n == 0) {
  33. return 0;
  34. }
  35. int rbits = Integer.numberOfTrailingZeros(n);
  36. int max = 0;
  37.  
  38. for (n >>>= (rbits + 1); n != 0; n >>>= (rbits + 1)) {
  39. rbits = Integer.numberOfTrailingZeros(n);
  40. max = Math.max(max, rbits);
  41. }
  42. return max;
  43. }
  44.  
  45. public static String toBinary(int v) {
  46. String bin = Integer.toBinaryString(v);
  47. return "00000000000000000000000000000000".substring(bin.length()) + bin;
  48. }
  49.  
  50. public static void main(String[] args) throws java.lang.Exception {
  51. for (int i = 0; i < 100; i++) {
  52. System.out.printf("Max gap in %4d (%s) is %d - %d\n", i, toBinary(i), maxGap(i), maxGapX(i));
  53. }
  54. }
  55. }
Success #stdin #stdout 0.09s 4386816KB
stdin
Standard input is empty
stdout
Max gap in    0 (00000000000000000000000000000000) is 0 - 0
Max gap in    1 (00000000000000000000000000000001) is 0 - 0
Max gap in    2 (00000000000000000000000000000010) is 0 - 0
Max gap in    3 (00000000000000000000000000000011) is 0 - 0
Max gap in    4 (00000000000000000000000000000100) is 0 - 0
Max gap in    5 (00000000000000000000000000000101) is 1 - 1
Max gap in    6 (00000000000000000000000000000110) is 0 - 0
Max gap in    7 (00000000000000000000000000000111) is 0 - 0
Max gap in    8 (00000000000000000000000000001000) is 0 - 0
Max gap in    9 (00000000000000000000000000001001) is 2 - 2
Max gap in   10 (00000000000000000000000000001010) is 1 - 1
Max gap in   11 (00000000000000000000000000001011) is 1 - 1
Max gap in   12 (00000000000000000000000000001100) is 0 - 0
Max gap in   13 (00000000000000000000000000001101) is 1 - 1
Max gap in   14 (00000000000000000000000000001110) is 0 - 0
Max gap in   15 (00000000000000000000000000001111) is 0 - 0
Max gap in   16 (00000000000000000000000000010000) is 0 - 0
Max gap in   17 (00000000000000000000000000010001) is 3 - 3
Max gap in   18 (00000000000000000000000000010010) is 2 - 2
Max gap in   19 (00000000000000000000000000010011) is 2 - 2
Max gap in   20 (00000000000000000000000000010100) is 1 - 1
Max gap in   21 (00000000000000000000000000010101) is 1 - 1
Max gap in   22 (00000000000000000000000000010110) is 1 - 1
Max gap in   23 (00000000000000000000000000010111) is 1 - 1
Max gap in   24 (00000000000000000000000000011000) is 0 - 0
Max gap in   25 (00000000000000000000000000011001) is 2 - 2
Max gap in   26 (00000000000000000000000000011010) is 1 - 1
Max gap in   27 (00000000000000000000000000011011) is 1 - 1
Max gap in   28 (00000000000000000000000000011100) is 0 - 0
Max gap in   29 (00000000000000000000000000011101) is 1 - 1
Max gap in   30 (00000000000000000000000000011110) is 0 - 0
Max gap in   31 (00000000000000000000000000011111) is 0 - 0
Max gap in   32 (00000000000000000000000000100000) is 0 - 0
Max gap in   33 (00000000000000000000000000100001) is 4 - 4
Max gap in   34 (00000000000000000000000000100010) is 3 - 3
Max gap in   35 (00000000000000000000000000100011) is 3 - 3
Max gap in   36 (00000000000000000000000000100100) is 2 - 2
Max gap in   37 (00000000000000000000000000100101) is 2 - 2
Max gap in   38 (00000000000000000000000000100110) is 2 - 2
Max gap in   39 (00000000000000000000000000100111) is 2 - 2
Max gap in   40 (00000000000000000000000000101000) is 1 - 1
Max gap in   41 (00000000000000000000000000101001) is 2 - 2
Max gap in   42 (00000000000000000000000000101010) is 1 - 1
Max gap in   43 (00000000000000000000000000101011) is 1 - 1
Max gap in   44 (00000000000000000000000000101100) is 1 - 1
Max gap in   45 (00000000000000000000000000101101) is 1 - 1
Max gap in   46 (00000000000000000000000000101110) is 1 - 1
Max gap in   47 (00000000000000000000000000101111) is 1 - 1
Max gap in   48 (00000000000000000000000000110000) is 0 - 0
Max gap in   49 (00000000000000000000000000110001) is 3 - 3
Max gap in   50 (00000000000000000000000000110010) is 2 - 2
Max gap in   51 (00000000000000000000000000110011) is 2 - 2
Max gap in   52 (00000000000000000000000000110100) is 1 - 1
Max gap in   53 (00000000000000000000000000110101) is 1 - 1
Max gap in   54 (00000000000000000000000000110110) is 1 - 1
Max gap in   55 (00000000000000000000000000110111) is 1 - 1
Max gap in   56 (00000000000000000000000000111000) is 0 - 0
Max gap in   57 (00000000000000000000000000111001) is 2 - 2
Max gap in   58 (00000000000000000000000000111010) is 1 - 1
Max gap in   59 (00000000000000000000000000111011) is 1 - 1
Max gap in   60 (00000000000000000000000000111100) is 0 - 0
Max gap in   61 (00000000000000000000000000111101) is 1 - 1
Max gap in   62 (00000000000000000000000000111110) is 0 - 0
Max gap in   63 (00000000000000000000000000111111) is 0 - 0
Max gap in   64 (00000000000000000000000001000000) is 0 - 0
Max gap in   65 (00000000000000000000000001000001) is 5 - 5
Max gap in   66 (00000000000000000000000001000010) is 4 - 4
Max gap in   67 (00000000000000000000000001000011) is 4 - 4
Max gap in   68 (00000000000000000000000001000100) is 3 - 3
Max gap in   69 (00000000000000000000000001000101) is 3 - 3
Max gap in   70 (00000000000000000000000001000110) is 3 - 3
Max gap in   71 (00000000000000000000000001000111) is 3 - 3
Max gap in   72 (00000000000000000000000001001000) is 2 - 2
Max gap in   73 (00000000000000000000000001001001) is 2 - 2
Max gap in   74 (00000000000000000000000001001010) is 2 - 2
Max gap in   75 (00000000000000000000000001001011) is 2 - 2
Max gap in   76 (00000000000000000000000001001100) is 2 - 2
Max gap in   77 (00000000000000000000000001001101) is 2 - 2
Max gap in   78 (00000000000000000000000001001110) is 2 - 2
Max gap in   79 (00000000000000000000000001001111) is 2 - 2
Max gap in   80 (00000000000000000000000001010000) is 1 - 1
Max gap in   81 (00000000000000000000000001010001) is 3 - 3
Max gap in   82 (00000000000000000000000001010010) is 2 - 2
Max gap in   83 (00000000000000000000000001010011) is 2 - 2
Max gap in   84 (00000000000000000000000001010100) is 1 - 1
Max gap in   85 (00000000000000000000000001010101) is 1 - 1
Max gap in   86 (00000000000000000000000001010110) is 1 - 1
Max gap in   87 (00000000000000000000000001010111) is 1 - 1
Max gap in   88 (00000000000000000000000001011000) is 1 - 1
Max gap in   89 (00000000000000000000000001011001) is 2 - 2
Max gap in   90 (00000000000000000000000001011010) is 1 - 1
Max gap in   91 (00000000000000000000000001011011) is 1 - 1
Max gap in   92 (00000000000000000000000001011100) is 1 - 1
Max gap in   93 (00000000000000000000000001011101) is 1 - 1
Max gap in   94 (00000000000000000000000001011110) is 1 - 1
Max gap in   95 (00000000000000000000000001011111) is 1 - 1
Max gap in   96 (00000000000000000000000001100000) is 0 - 0
Max gap in   97 (00000000000000000000000001100001) is 4 - 4
Max gap in   98 (00000000000000000000000001100010) is 3 - 3
Max gap in   99 (00000000000000000000000001100011) is 3 - 3