From 657ae9754af015b53915a9bf3ffa8e32c3941ce3 Mon Sep 17 00:00:00 2001 From: "sahil.samantaray" Date: Mon, 4 Oct 2021 22:56:24 +0530 Subject: [PATCH 1/2] Implemented MinMax solution using Stack --- .../Stacks/MaximumMinimumWindow.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 DataStructures/Stacks/MaximumMinimumWindow.java diff --git a/DataStructures/Stacks/MaximumMinimumWindow.java b/DataStructures/Stacks/MaximumMinimumWindow.java new file mode 100644 index 000000000000..e321e65d7965 --- /dev/null +++ b/DataStructures/Stacks/MaximumMinimumWindow.java @@ -0,0 +1,95 @@ +package DataStructures.Stacks; + +import java.util.Arrays; +import java.util.Stack; + +/** + * Given an integer array. The task is to find the maximum of the minimum of every window size in the array. + * Note: Window size varies from 1 to the size of the Array. + *

+ * For example, + *

+ * N = 7 + * arr[] = {10,20,30,50,10,70,30} + *

+ * So the answer for the above would be : 70 30 20 10 10 10 10 + *

+ * We need to consider window sizes from 1 to length of array in each iteration. + * So in the iteration 1 the windows would be [10], [20], [30], [50], [10], [70], [30]. + * Now we need to check the minimum value in each window. Since the window size is 1 here the minimum element would be the number itself. + * Now the maximum out of these is the result in iteration 1. + * In the second iteration we need to consider window size 2, so there would be [10,20], [20,30], [30,50], [50,10], [10,70], [70,30]. + * Now the minimum of each window size would be [10,20,30,10,10] and the maximum out of these is 30. + * Similarly we solve for other window sizes. + * + * @author sahil + */ +public class MaximumMinimumWindow { + + /** + * This function contains the logic of finding maximum of minimum for every window size + * using Stack Data Structure. + * + * @param arr Array containing the numbers + * @param n Length of the array + * @return result array + */ + public static int[] calculateMaxOfMin(int[] arr, int n) { + Stack s = new Stack<>(); + int left[] = new int[n + 1]; + int right[] = new int[n + 1]; + for (int i = 0; i < n; i++) { + left[i] = -1; + right[i] = n; + } + + for (int i = 0; i < n; i++) { + while (!s.empty() && arr[s.peek()] >= arr[i]) + s.pop(); + + if (!s.empty()) + left[i] = s.peek(); + + s.push(i); + } + + while (!s.empty()) + s.pop(); + + for (int i = n - 1; i >= 0; i--) { + while (!s.empty() && arr[s.peek()] >= arr[i]) + s.pop(); + + if (!s.empty()) + right[i] = s.peek(); + + s.push(i); + } + + int ans[] = new int[n + 1]; + for (int i = 0; i <= n; i++) + ans[i] = 0; + + for (int i = 0; i < n; i++) { + int len = right[i] - left[i] - 1; + + ans[len] = Math.max(ans[len], arr[i]); + } + + for (int i = n - 1; i >= 1; i--) + ans[i] = Math.max(ans[i], ans[i + 1]); + + // Print the result + for (int i = 1; i <= n; i++) + System.out.print(ans[i] + " "); + return ans; + } + + public static void main(String args[]) { + int[] arr = new int[]{10, 20, 30, 50, 10, 70, 30}; + int[] target = new int[]{70, 30, 20, 10, 10, 10, 10}; + int[] res = calculateMaxOfMin(arr, arr.length); + assert Arrays.equals(target, res); + } + +} From aee0fbf828b90cbd9ede36ed078870aced960df2 Mon Sep 17 00:00:00 2001 From: "sahil.samantaray" Date: Wed, 6 Oct 2021 15:46:21 +0530 Subject: [PATCH 2/2] Added implementation for square root using Binary Search --- Searches/SquareRootBinarySearch.java | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Searches/SquareRootBinarySearch.java diff --git a/Searches/SquareRootBinarySearch.java b/Searches/SquareRootBinarySearch.java new file mode 100644 index 000000000000..1790f8abc710 --- /dev/null +++ b/Searches/SquareRootBinarySearch.java @@ -0,0 +1,59 @@ +package Searches; + +import java.util.Scanner; + +/** + * Given an integer x, find the square root of x. If x is not a perfect square, then return floor(√x). + *

+ * For example, + * if x = 5, The answer should be 2 which is the floor value of √5. + *

+ * The approach that will be used for solving the above problem is not going to be a straight forward Math.sqrt(). + * Instead we will be using Binary Search to find the square root of a number in the most optimised way. + * + * @author sahil + */ +public class SquareRootBinarySearch { + + /** + * This is the driver method. + * + * @param args Command line arguments + */ + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a number you want to calculate square root of : "); + int num = sc.nextInt(); + long ans = squareRoot(num); + System.out.println("The square root is : " + ans); + } + + /** + * This function calculates the floor of square root of a number. + * We use Binary Search algorithm to calculate the square root + * in a more optimised way. + * + * @param num Number + * @return answer + */ + private static long squareRoot(long num) { + if (num == 0 || num == 1) { + return num; + } + long l = 1; + long r = num; + long ans = 0; + while (l <= r) { + long mid = l + (r - l) / 2; + if (mid == num / mid) + return mid; + else if (mid < num / mid) { + ans = mid; + l = mid + 1; + } else { + r = mid - 1; + } + } + return ans; + } +}