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. Scanner sc = new Scanner(System.in);
  13. int n=sc.nextInt();
  14. Queue<Integer> q= new LinkedList<Integer>();
  15. Queue<Integer> q1= new LinkedList<Integer>();
  16. q.offer(0);
  17. for(int i=1;i<=n;i++)
  18. {
  19. int j=i;
  20. while(!q.isEmpty())
  21. {
  22. int x=q.poll();
  23. int res1=x-i;
  24. int res2=x+i;
  25. q1.offer(res1);
  26. q1.offer(res2);
  27.  
  28. if(q1.contains(n))
  29. {
  30. System.out.println(i);
  31. return;
  32. }
  33. }
  34. q.addAll(q1);
  35. q1= new LinkedList<Integer>();
  36. }
  37. }
  38. }
Success #stdin #stdout 0.06s 321344KB
stdin
10
stdout
4