## Which approach to use when finding min and max value in array?

I wonder what is the best way to find min and max value in array. I use two approaches (my array is `a`

with size `size`

):

Approach 1:

int min = 0, max = 0; for(int i = 0; i < size; i++) { if(a[i] > max || i == 0) max = a[i]; if(a[i] < min || i == 0) min = a[i]; }

Approach 2:

int min = INT_MAX, max = INT_MIN; for(int i = 0; i < size; i++) { if(a[i] > max) max = a[i]; if(a[i] < min) min = a[i]; }

Approach 3:

int min = a[0], max = a[0]; for(int i = 1; i < size; i++) { if(a[i] > max) max = a[i]; if(a[i] < min) min = a[i]; }

Approach 2 seems a little bit more optimal to my newbie eye (we don't have to evaluate `i == 0`

2 times every loop iteration. However, I am afraid that playing with these limit values might go wrong. This is solved in approach 3, though. Which is the most optimal of the three and why?

Thanks for all replies!

Try to go for the 3rd option. It is more optimal and then you don't have to deal with the limits values.

**Which approach to use when finding min and max value in array ,** Try to go for the 3rd option. It is more optimal and then you don't have to deal with the limits values. Finding the Min Value in the entire array To find the minimum value inside the array you have to use the numpy.min() method and pass the array. #Minimum Element in the 2D- Array min_2d = np.min(array_2d) print("The minimum value for the 2D-array:",min_2d) Min Value in a 2D Numpy Array

Approach 1 suites all cases, so if you are writing code with no regard to special context, I would use it.

Approach 2 depends on type of variable. If you change it from `int32`

to whatever, you would have to rewrite the program. And btw for unsigned types `0`

is minimum value and no special constant can be defined.

Approach 3 requires that you sequence (array) is not empty and cannot be used without additional length-checking.

I like this modification of Approach 3:

// assuming size is of size_t and can't be negative int min = size ? a[0] : 0; int max = min; for(int i = 1; i < size; i++) { if(a[i] > max) max = a[i]; if(a[i] < min) min = a[i]; }

##### UPD

I am really surprised to see people think Approach 1 wont work on negative numbers. It **will** because on first iteration, when `i==0`

is checked, we always init `min`

and `max`

with first element. In case of doubts I recommend running this code on negative sequence - it works (I've checked)

**Maximum and minimum of an array using minimum number of ,** Write a C function to return minimum and maximum in an array. In this method, total number of comparisons is 1 + 2(n-2) in worst case and 1 + n – 2 in best case. Please write comments if you find any bug in the above programs/algorithms You can find the minimum and maximum values of an array using for loops −ExampleLive Demopublic class MinAndMax { public int max(int [] array) {

2nd and 3rd approaches are better approaches. Personally I feel 3rd one is better solution than 2nd solution.

**Min-Max Range Queries in Array,** Complexity for this approach will be O(queries * n). Efficient C++ program to find minimum and maximum using segment tree a given range of array indexes. Using a for loop or .reduce () E ither a for loop or the Array.prototype.reduce () method can be used to find the minimum and maximum numbers in an array: Brandon Morelli reported previously on

**Java,** In this example we are finding out the maximum and minimum values from an int array. Calling getMax() method for getting max value int max = getMax(array); Given an array of integers, find minimum and maximum element present in it by doing minimum comparisons by using divide and conquer technique. For example, Input: arr = [5, 7, 2, 4, 9, 6] Output: The minimum element in the array is 2 The maximum element in the array is 9 We can easily solve this problem by using Divide and conquer (D&C). The idea is to recursively divide the array into two equal parts and update the maximum and minimum of the whole array in recursion itself by passing

**Three ways to find minimum and maximum values in a Java array of ,** Using Arrays.sort method to Find Maximum and Minimum Values in an Array. int[] nums={6,-1,-2,-3,0,1,2,3,4};. Arrays.sort(nums);. System.out.println("Minimum Hence, having shorthand for several utilities can be beneficial. One such utility, to find maximum and minimum element in array is explained in this article using “ aslist () “. aslist () type casts a list from the array passed in its argument. This function is defined in “ Java.utils.Arrays “. To get the minimum or maximum value from the array we can use the Collections.min () and Collections.max () methods.

**Find minimum and maximum value in an array,** Given an array A[] of size n, you need to find the maximum and minimum and Conquer: Tournament Method; Comparison in pairs: Increment the loop by 2 Find the smallest and second smallest element in the array using Python examples to find the largest (or the smallest) item in a collection (e.g. list, set or array) of comparable elements using max () and min () methods. 1. Python max () function max () function is used to –