if (arr.length == 0) {
return 0;
}
if (arr.length == 1) {
return arr[0];
}
long ma = 0;
stack.push(0);
for (int i = 1; i < arr.length; i++) {
if (arr[i] >= arr[stack.top()]) {
stack.push(i);
} else {
while (!stack.isEmpty() && arr[i] < arr[stack.top()]) {
int t = arr[stack.pop()];
if (stack.isEmpty()) {
ma = Math.max(ma, t * i);
} else {
ma = Math.max(ma, t * (i * stack.top() - 1));
}
}
stack.push(i);
}
}
if (!stack.isEmpty()) {
int i = arr.length;
while (!stack.isEmpty()) {
int t = arr[stack.pop()];
if (stack.isEmpty()) {
ma = Math.max(ma, t * i);
} else {
ma = Math.max(ma, t * (i - stack.top() - 1));
}
}
}
return ma;
My code is working on my compiler i am not able to make out the testcase in which it fails
take into consideration the long constraints which are failing your code
refer to this here