Repository: TheAlgorithms/Java
Pull request #3044 (2022-05-04 16:23:56)

'Fixes #2957'
### **Describe your change:**

- [x] Add an algorithm?
- [] Fix a bug or typo in an existing algorithm?
- [] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


siddhant2002: @siriak please check the pr
siddhant2002: @siriak please check the updated pr

Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java b/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java
new file mode 100644
index 00000000..7af1192a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/stacks/CalculateMaxOfMin.java
@@ -0,0 +1,37 @@
+/** Author : Siddhant Swarup Mallick
+ * Github : https://github.com/siddhant2002
+ */
+
+/** Program description - Given an integer array. The task is to find the maximum of the minimum of the array */
+package com.thealgorithms.datastructures.stacks;
+
+import java.util.*;
+
+public class CalculateMaxOfMin {
+    public static int calculateMaxOfMin(int[] a) {
+        int n = a.length;
+        int[] ans = new int[n];
+        int[] arr2 = Arrays.copyOf(a, n);
+        Arrays.sort(arr2);
+        int maxNum = arr2[arr2.length - 1];
+        ans[0] = maxNum;
+        int index = 1;
+        while (index != ans.length) {
+            int[] minimums = new int[n - index];
+            for (int i = 0; i < n - index; i++) {
+                int[] windowArray = Arrays.copyOfRange(a, i, i + index + 1);
+                Arrays.sort(windowArray);
+                int minNum = windowArray[0];
+                minimums[i] = minNum;
+            }
+            Arrays.sort(minimums);
+            ans[index] = minimums[minimums.length - 1];
+            index += 1;
+        }
+        return ans[0];
+    }
+}
+/**
+ * Given an integer array. The task is to find the maximum of the minimum of the
+ * given array
+ */
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java
new file mode 100644
index 00000000..c5a02822
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/CalculateMaxOfMinTest.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.others;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import com.thealgorithms.datastructures.stacks.CalculateMaxOfMin;
+
+public class CalculateMaxOfMinTest {
+	@Test
+	void testForOneElement() {
+		int a[] = { 10, 20, 30, 50, 10, 70, 30 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == 70);
+	}
+
+	@Test
+	void testForTwoElements() {
+		int a[] = { 5, 3, 2, 6, 3, 2, 6 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == 6);
+	}
+
+	@Test
+	void testForThreeElements() {
+		int a[] = { 10, 10, 10, 10, 10, 10, 10 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == 10);
+	}
+
+	@Test
+	void testForFourElements() {
+		int a[] = { 70, 60, 50, 40, 30, 20 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == 70);
+	}
+
+	@Test
+	void testForFiveElements() {
+		int a[] = { 50 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == 50);
+	}
+
+	@Test
+	void testForSixElements() {
+		int a[] = { 1, 4, 7, 9, 2, 4, 6 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == 9);
+	}
+
+	@Test
+	void testForSevenElements() {
+		int a[] = { -1, -5, -7, -9, -12, -14 };
+		int k = CalculateMaxOfMin.calculateMaxOfMin(a);
+		assertTrue(k == -1);
+	}
+}
\ No newline at end of file