0

Can anybody explain the static Pair get Min Max method? Why are we using the Inner class name Pair in front of the get Min Max method? I am having trouble finding out info about this. Can anybody explain? I'm new to java. Code:

public class Geeks {

    static class Pair {
 
        int min;
        int max;
    }
 
    static Pair getMinMax(int arr[], int low, int high) {
        Pair minmax = new Pair();
        Pair mml = new Pair();
        Pair mmr = new Pair();
        int mid;
 
        // If there is only one element
        if (low == high) {
            minmax.max = arr[low];
            minmax.min = arr[low];
            return minmax;
        }
 
        /* If there are two elements */
        if (high == low + 1) {
            if (arr[low] > arr[high]) {
                minmax.max = arr[low];
                minmax.min = arr[high];
            } else {
                minmax.max = arr[high];
                minmax.min = arr[low];
            }
            return minmax;
        }
 
        /* If there are more than 2 elements */
        mid = (low + high) / 2;
        mml = getMinMax(arr, low, mid);
        mmr = getMinMax(arr, mid + 1, high);
 
        /* compare minimums of two parts*/
        if (mml.min < mmr.min) {
            minmax.min = mml.min;
        } else {
            minmax.min = mmr.min;
        }
 
        /* compare maximums of two parts*/
        if (mml.max > mmr.max) {
            minmax.max = mml.max;
        } else {
            minmax.max = mmr.max;
        }
 
        return minmax;
    }
 
    /* Driver program to test above function */
    public static void main(String args[]) {
        int arr[] = {1000, 11, 445, 1, 330, 3000};
        int arr_size = 6;
        Pair minmax = getMinMax(arr, 0, arr_size - 1);
        System.out.printf("\nMinimum element is %d", minmax.min);
        System.out.printf("\nMaximum element is %d", minmax.max);
 
    }
}
4
  • "Why are we using Inner class name Pair in front of the the get Min Max method" Pair is the return type, no different to if you returned int or String. Commented Aug 23, 2021 at 8:56
  • There is an object type called Pair which has a min and a max. Then you have a static method (meaning it can be called even if you don't have an instance of the parent class) which takes some inputs out of which will create a Pair that contains these two information (min and max of your inputs) Commented Aug 23, 2021 at 8:57
  • "Inner class" Pair isn't an inner class, it's a nested class. Inner classes are necessarily non-static. Commented Aug 23, 2021 at 8:57
  • 1
    It tries to perform a binary search / divide and conquer which does not make any sense here since the input is unordered and you will check element once anyway, a regular loop would yield the exact same output in the same time, probably even faster because it does not do the unnecessary allocation and recursion Commented Aug 23, 2021 at 8:58

3 Answers 3

1

Not answering the question, but to @luk2302's point: this method is really over-complicated, and can be written far more easily with a loop:

static Pair getMinMax(int arr[]) {
  Pair result = new Pair();
  result.min = Integer.MAX_VALUE;
  result.max = Integer.MIN_VALUE;

  for (int a : arr) {
    result.min = Math.min(result.min, a);
    result.max = Math.max(result.max, a);
  }

  return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@luk2302 removed the low/high. No need for those if you're not recursing.
0

The reason why
Pair minmax = new Pair() is used because the function should return the minimum element in the array and the maximum element in the array, so the one who wrote this snippet decided to create Pair class to make more intuitive to return it as object with min and max attribute instead of returning an array of length 2(which is a valid also).

Comments

0

I'd always recommend following the syntax. In Java the syntax to write a function is:

<access modifier> <return type> <functionName>(parameter_type parameter_name){
   // function body
}

enter image description here

In the code snippet you have shared:

  • Access Modifier: static
  • Return Type: Pair
  • Function Name: getMinMax
  • Params: int arr[], int low and int high

Hence the function returns a Pair type instead of an array with 2 values. It pre-fills the Pair's min and max fields with the answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.