Ad
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Your right, I hadn't seen the problem from that angle. So you mean it would be better to do something like the following one :

    for(int i : arr){
        if(i < min) min = i;
        if(i > max) max = i;
    }
    
  • Default User Avatar

    Great. Thus, as you can see, you can much more simplify your algorithm :p

  • Default User Avatar

    I don't understand why you need to iterate through all array elements so many times while you can just do something like that :

     public static int[] findHighLow(int[] arr){
            int largest = arr[0], smallest = arr[0];
    
            for(int i = 1; i < arr.length; i++){
                largest = Math.max(arr[i], largest);
                smallest = Math.min(arr[i], smallest);
            }
            return new int[]{smallest, largest};
        }