fork(5) 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. findMinimumSteps(11);
  13. }
  14. private static void findMinimumSteps(int dest)
  15. {
  16. if(dest==0)
  17. {
  18. System.out.println(dest);
  19. return;
  20. }
  21. if(dest<0)
  22. dest*=-1;
  23.  
  24. int i = (int)Math.ceil((Math.sqrt(1+8*dest)-1)/2);
  25. int sum=i*(i+1)/2;
  26. if(dest%2==0)
  27. {
  28. if(i%2==0 && sum%2==1)
  29. {
  30. System.out.println(i+1);
  31. }
  32. else if(i%2==1 && sum%2==1)
  33. {
  34. System.out.println(i+2);
  35. }
  36. else
  37. {
  38. System.out.println(i);
  39. }
  40. }
  41. else
  42. {
  43. if(i%2==0 && sum%2==0)
  44. {
  45. System.out.println(i+1);
  46. }
  47. else if(i%2==1 && sum%2==0)
  48. {
  49. System.out.println(i+2);
  50. }
  51. else
  52. {
  53. System.out.println(i);
  54. }
  55. }
  56.  
  57. }
  58. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
5