Untitled
public class tUf { public static int minProductSubArray(int[] arr) { int n = arr.length; // size of array int pre = 1, suff = 1; int ans = Integer.MAX_VALUE; // Track the minimum product for (int i = 0; i < n; i++) { if (pre == 0) pre = 1; if (suff == 0) suff = 1; pre *= arr[i]; suff *= arr[n - i - 1]; ans = Math.min(ans, Math.min(pre, suff)); // Use Math.min to track the minimum } return ans; } public static void main(String[] args) { int[] arr = {1, 2, -3, 0, -4, -5}; int answer = minProductSubArray(arr); System.out.println("The minimum product subarray is: " + answer); } }
Leave a Comment