# how to get the minimum and maximum value of arrays inside an array?

• Last Update :
• Techknowledgy :

We declare and initialize variables max and min to store maximum and minimum.,By the end of loop, minimum and maximum values of the array will be stored in variables min and max. ,Declare the max and min variables and check for the array size:,We recursively calculate and store the maximum and minimum for the left part i.e. leftMinMax[2] = findMinMax(X, l, mid)

```int[] findMinMax(int X[], int n) {
int max = X[0]
int min = X[0]
for (int i = 1; i < n; i = i + 1) {
if (X[i] > max)
max = X[i]
else if (X[i] < min)
min = X[i]
}
int maxMin[2] = {
max,
min
}
return maxMin
}```
```if (l == r) {
max = X[l]
min = X[l]
} else if (l + 1 == r) {
if (X[l] < X[r]) {
max = X[r]
min = X[l]
} else {
max = X[l]
min = X[r]
}
}```
```if (leftMinMax[0] > rightMinMax[0])
max = leftMinMax[0]
else
max = rightMinMax[0]

if (leftMinMax[1] < rightMinMax[1])
min = leftMinMax[1]
else
min = rightMinMax[1]```
```int[] findMinMax(int X[], int l, int r) {
int max, min
if (l == r) {
max = X[l]
min = X[l]
} else if (l + 1 == r) {
if (X[l] < X[r]) {
max = X[r]
min = X[l]
} else {
max = X[l]
min = X[r]
}
} else {
int mid = l + (r - l) / 2
int leftMinMax[2] = findMinMax(X, l, mid)
int rightMinMax[2] = findMinMax(X, mid + 1, r)
if (leftMinMax[0] > rightMinMax[0])
max = leftMinMax[0]
else
max = rightMinMax[0]
if (leftMinMax[1] < rightMinMax[1])
min = leftMinMax[1]
else
min = rightMinMax[1]
}
int maxMin[2] = {
max,
min
}
return maxMin
}```
```if (n % 2 != 0) {
max = X[0]
min = X[0]
i = 1
} else {
if (X[0] < X[1]) {
max = X[1]
min = X[0]
} else {
max = X[0]
min = X[1]
}
i = 2
}```
```while (i < n) {
if (X[i] < X[i + 1]) {
if (X[i] < min)
min = X[i]
if (X[i + 1] > max)
max = X[i + 1]
} else {
if (X[i] > max)
max = X[i]
if (X[i + 1] < min)
min = X[i + 1]
}
i = i + 2
}```

Suggestion : 2

IIUC, you can do `stack`:

```np.vstack([d
for d in data
]).min(axis = 0)```

Output:

`array([619, 465, 529])`

You can try the following to get the min/max if the columns are the same.

```In[28]: test = [
[
[619, 502, 551],
...: [624, 504, 551]
],
...: [
[624, 498, 531],
...: [628, 502, 529]
],
...: [
[619, 496, 557],
...: [892, 508, 559]
],
...: [
[619, 494, 561],
...: [895, 506, 559],
...: [902, 512, 559]
],
...: [
[619, 494, 559],
...: [918, 510, 567]
],
...: [
[619, 493, 561],
...: [931, 512, 561],
...: [932, 512, 561]
],
...: [
[619, 494, 561],
...: [942, 510, 559]
],
...: [
[619, 493, 561],
...: [620, 493, 559],
...: [948, 512, 561]
],
...: [
[619, 494, 591],
...: [752, 542, 633]
],
...: [
[626, 465, 567],
...: [766, 532, 633]
]
]

In[29]: test1 = []

In[30]: [test1.append(t) for t1 in test
for t in t1
]

In[31]: test1
Out[31]: [
[619, 502, 551],
[624, 504, 551],
[624, 498, 531],
[628, 502, 529],
[619, 496, 557],
[892, 508, 559],
[619, 494, 561],
[895, 506, 559],
[902, 512, 559],
[619, 494, 559],
[918, 510, 567],
[619, 493, 561],
[931, 512, 561],
[932, 512, 561],
[619, 494, 561],
[942, 510, 559],
[619, 493, 561],
[620, 493, 559],
[948, 512, 561],
[619, 494, 591],
[752, 542, 633],
[626, 465, 567],
[766, 532, 633]
]

In[32]: np.amin(test1, None)
Out[32]: 465

In[33]: np.max(test1, None)
Out[33]: 948```

If you're looking for just a column you can make use of the indexing of numpy arrays. e.g.

```arr = np.asarray(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
print(arr)[[1 2 3]
[4 5 6]
[7 8 9]]```

with slicing you can get columns as subarrays like the following example

`print(arr[: , 0])[1 4 7]`

Suggestion : 3

Last Updated : 21 Jun, 2022,GATE CS 2021 Syllabus

```Minimum number of array is: 2
Maximum number of array is: 9```

Suggestion : 4

If array size is 1, return the element as both max and min,If array size is 2, compare the two elements and return maximum and minimum,If odd, initialize min and max to the first element,Find the minimum element in a sorted and rotated array

Pseudo-Code
```int[] getMinMax(int A[], int n) {
int max = A[0]
int min = A[0]
for (i = 1 to n - 1) {
if (A[i] > max)
max = A[i]
else if (A[i] < min)
min = A[i]
}
// By convention, let ans[0] = maximum and ans[1] = minimum
int ans[2] = {
max,
min
}
return ans
}```
Pseudo Code
```int[] findMinMax(int A[], int start, int end) {
int max;
int min;
if (start == end) {
max = A[start]
min = A[start]
} else if (start + 1 == end) {
if (A[start] < A[end]) {
max = A[end]
min = A[start]
} else {
max = A[start]
min = A[end]
}
} else {
int mid = start + (end - start) / 2
int left[] = findMinMax(A, start, mid)
int right[] = findMinMax(A, mid + 1, end)
if (left[0] > right[0])
max = left[0]
else
max = right[0]
if (left[1] < right[1])
min = left[1]
else
min = right[1]
}
// By convention, we assume ans[0] as max and ans[1] as min
int ans[2] = {
max,
min
}
return ans
}```

For counting the number of comparisons, since this is a recursive function, let us define the recurrence relation :

```T(n) = 2 T(n / 2) + 2
T(2) = 1
T(1) = 0
We can solve this recurrence relation by master method / recursion tree method.
if n is a power of 2
T(n) = 3n / 2 - 2```