Min and Max
  1. import java.util.*;
  2. import javafx.util.*;
  3.  
  4. public class M
  5. {
  6. public static Pair<Integer, Integer> getMinMax(int [] array)
  7. {
  8. int min, max;
  9. int index;
  10.  
  11. if(array.length%2 == 0)
  12. {
  13. if(array[0] < array[1])
  14. {
  15. min = array[0];
  16. max = array[1];
  17. }
  18. else
  19. {
  20. min = array[1];
  21. max = array[0];
  22. }
  23. index = 2;
  24.  
  25. }
  26. else
  27. {
  28. min = max = array[0];
  29. index = 1;
  30. }
  31.  
  32. for(int i = index; i < array.length-1; i++)
  33. {
  34. if(array[i] < array[i+1])
  35. {
  36. if(min > array[i])
  37. min = array[i];
  38. if(array[i+1] > max)
  39. max = array[i+1];
  40. }
  41. else
  42. {
  43. if(min > array[i+1])
  44. min = array[i+1];
  45. if(max < array[i])
  46. max = array[i];
  47. }
  48. }
  49.  
  50. return (new Pair<Integer, Integer>(min, max));
  51. }
  52.  
  53. public static int [] getArray(int n)
  54. {
  55. int [] array = new int[n];
  56. Random r = new Random();
  57.  
  58. for(int i = 0; i < n; i++)
  59. array[i] = r.nextInt(100);
  60.  
  61. return array;
  62. }
  63.  
  64. public static void main(String args[])
  65. {
  66. int [] array = getArray(100);
  67.  
  68. Pair<Integer, Integer> p = getMinMax(array);
  69.  
  70. System.out.println("Min: "+p.getKey()+" Max: "+p.getValue());
  71. }
  72. }
Reference