Madhu1

<p>public class Main<br /> {<br /> &nbsp; &nbsp; public static int maxSubarrayValue(int[] mea) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; // Initialize val to a very small number<br /> &nbsp; &nbsp; &nbsp; &nbsp; int val = -123456789;<br /> &nbsp; &nbsp; &nbsp; &nbsp; int n = mea.length;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; // Iterate over all possible subarray sizes<br /> &nbsp; &nbsp; &nbsp; &nbsp; for (int count = 1; count &lt;= n; count++) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate over all possible starting indices for subarrays of the current size<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt;= n - count; i++) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Extract the subarray<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] subarray = new int[count];<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.arraycopy(mea, i, subarray, 0, count);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Find the minimum value in the subarray<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int minVal = findMin(subarray);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Calculate the value for this subarray<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int currentVal = count * minVal;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Update the maximum value<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val = Math.max(val, currentVal);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Print debug information<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Checking the min in &quot; + java.util.Arrays.toString(subarray));<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;min is &quot; + minVal + &quot; actual val is &quot; + currentVal);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; // If val is still the initial very small number, return 0<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val == -123456789) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;<br /> &nbsp; &nbsp; &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; return val;<br /> &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; private static int findMin(int[] array) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; int min = array[0];<br /> &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i &lt; array.length; i++) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i] &lt; min) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = array[i];<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; return min;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp;&nbsp;<br /> &nbsp;&nbsp; &nbsp;public static void main(String[] args) {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int[] mea = {10,80,30,500,70,40,20};<br /> &nbsp; &nbsp; &nbsp; &nbsp; int result = maxSubarrayValue(mea);<br /> &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Max value: &quot; + result);<br /> &nbsp;&nbsp; &nbsp;}<br /> }</p>