Skip to content

Commit 10f41ee

Browse files
authored
Add max of mins algorithm (TheAlgorithms#3044)
1 parent 9b5dca5 commit 10f41ee

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
/** Program description - Given an integer array. The task is to find the maximum of the minimum of the array */
6+
package com.thealgorithms.datastructures.stacks;
7+
8+
import java.util.*;
9+
10+
public class CalculateMaxOfMin {
11+
public static int calculateMaxOfMin(int[] a) {
12+
int n = a.length;
13+
int[] ans = new int[n];
14+
int[] arr2 = Arrays.copyOf(a, n);
15+
Arrays.sort(arr2);
16+
int maxNum = arr2[arr2.length - 1];
17+
ans[0] = maxNum;
18+
int index = 1;
19+
while (index != ans.length) {
20+
int[] minimums = new int[n - index];
21+
for (int i = 0; i < n - index; i++) {
22+
int[] windowArray = Arrays.copyOfRange(a, i, i + index + 1);
23+
Arrays.sort(windowArray);
24+
int minNum = windowArray[0];
25+
minimums[i] = minNum;
26+
}
27+
Arrays.sort(minimums);
28+
ans[index] = minimums[minimums.length - 1];
29+
index += 1;
30+
}
31+
return ans[0];
32+
}
33+
}
34+
/**
35+
* Given an integer array. The task is to find the maximum of the minimum of the
36+
* given array
37+
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.others;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import com.thealgorithms.datastructures.stacks.CalculateMaxOfMin;
6+
7+
public class CalculateMaxOfMinTest {
8+
@Test
9+
void testForOneElement() {
10+
int a[] = { 10, 20, 30, 50, 10, 70, 30 };
11+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
12+
assertTrue(k == 70);
13+
}
14+
15+
@Test
16+
void testForTwoElements() {
17+
int a[] = { 5, 3, 2, 6, 3, 2, 6 };
18+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
19+
assertTrue(k == 6);
20+
}
21+
22+
@Test
23+
void testForThreeElements() {
24+
int a[] = { 10, 10, 10, 10, 10, 10, 10 };
25+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
26+
assertTrue(k == 10);
27+
}
28+
29+
@Test
30+
void testForFourElements() {
31+
int a[] = { 70, 60, 50, 40, 30, 20 };
32+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
33+
assertTrue(k == 70);
34+
}
35+
36+
@Test
37+
void testForFiveElements() {
38+
int a[] = { 50 };
39+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
40+
assertTrue(k == 50);
41+
}
42+
43+
@Test
44+
void testForSixElements() {
45+
int a[] = { 1, 4, 7, 9, 2, 4, 6 };
46+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
47+
assertTrue(k == 9);
48+
}
49+
50+
@Test
51+
void testForSevenElements() {
52+
int a[] = { -1, -5, -7, -9, -12, -14 };
53+
int k = CalculateMaxOfMin.calculateMaxOfMin(a);
54+
assertTrue(k == -1);
55+
}
56+
}

0 commit comments

Comments
 (0)