fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.TreeSet;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10.  
  11. {
  12.  
  13. public static int Bigger(TreeSet<Integer> t, int n ){
  14. if(t.isEmpty()){
  15. return -2;
  16. }
  17. Integer v1 = t.higher(n);
  18. if(v1 == null){
  19. return -1;
  20. }
  21.  
  22. return v1;
  23. }
  24.  
  25. public static int Smaller(TreeSet<Integer> t, int n){
  26. if(t.isEmpty()){
  27. return -2;
  28. }
  29.  
  30. Integer v1 = t.floor(n);
  31. if(v1 == null){
  32. return -1;
  33. }
  34. if(v1 == n){
  35. v1 = t.lower(n);
  36. if(v1 == null){
  37. return -1;
  38. }
  39. }
  40. return v1;
  41. }
  42.  
  43. public static void main (String[] args) throws java.lang.Exception
  44. {
  45. // your code goes here
  46.  
  47. TreeSet<Integer> tree = new TreeSet<>();
  48.  
  49. tree.add(5);
  50. tree.add(3);
  51. tree.add(2);
  52. tree.add(10);
  53. tree.add(1);
  54.  
  55. int v1 = Bigger(tree,5);
  56. System.out.println(v1 + " ");
  57.  
  58. v1 = Smaller(tree,3);
  59. System.out.println(v1+ " " );
  60.  
  61.  
  62. }
  63. }
Success #stdin #stdout 0.15s 55848KB
stdin
Standard input is empty
stdout
10 
2