Issue with contest test case for Maximum Product Subarray question

Hi, I have a wrong answer error for test case 2 for Maximum product subarray question: CodeDrills

It says wrong answer for test case 2, but this is a hidden test case and I can’t crack my head around why this piece of code fails. It should be correct as I have verified it on other platforms with this similar question. Please take a look if this is an issue with the platform’s test case.

Edit: I have taken a look at the editorial for this and while I am not very familiar with c++, I believe the suggested code is wrong, it only takes into account positive products and ignores cases whereby the maximum product is formed by negatives values. Again, please check this as it is incredibly frustrating.

import java.util.*;

class MaximumProductSubarray {
	// DO NOT read from stdin or write to stdout
	// Input is given as function argument
	// Output is taken as the function return value
	public int getMaximumProduct(ArrayList<Integer> nums) {
		if (nums == null || nums.size() == 0) {
			return 0;
		}
		// Code here
		int[][] cache = new int[nums.size()][2];
		cache[0] = new int[]{nums.get(0), nums.get(0)};
		int res = cache[0][0];
		for (int i=1; i<nums.size(); i++) {
			int p1 = cache[i-1][1]*nums.get(i), p2 = cache[i-1][0]*nums.get(i);
			int min = Math.min(Math.min(nums.get(i), p1), p2);
			int max = Math.max(Math.max(nums.get(i), p1), p2);
			cache[i] = new int[]{max, min};
			res = Math.max(res, max);
		}
		
		return res;
	}
}

1 Like

Hello, sorry for the inconvenience. There was a test case which was exceeding integer limit and hence the verdict. We have corrected the test cases.

1 Like