Here is my code.I am getting correct output but when i submit it ,it is showing wrong answer.please tell me where is the wrong in my algorithm.
#include <stdio.h>
int MAXSIZE = 200001;
int stack[200001];
int top = -1;
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == MAXSIZE)
return 1;
else
return 0;
}
int peek() {
return stack[top];
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf(“Could not retrieve data, Stack is empty.\n”);
}
}
void push(long long int data) {
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf(“Could not insert data, Stack is full.\n”);
}
}
int max(int a,int b) {
if (a>b) {
return a;
}
else {
return b;
}
}
void largestarea(long long int a[],long long int n) {
long long int ls[n],rs[n],i;
for (i=0;i<n;i++) {
while (!isempty()) {
if (a[peek()]>=a[i]) {
pop();
}
else {
break;
}
}
if (isempty()) {
ls[i]=0;
}
else {
ls[i]=peek()+1;
}
push(i);
}
while(!isempty()) {
pop();
}
for (i=n-1;i>=0;i–) {
while(!isempty()) {
if (a[peek()]>=a[i]) {
pop();
}
else {
break;
}
}
if (isempty()) {
rs[i]=n-1;
}
else {
rs[i]=peek()-1;
}
push(i);
}
long long int max_area=0;
for (i=0;i<n;i++) {
max_area=max(max_area,a[i]*(rs[i]-ls[i]+1));
}
printf("%lld\n",max_area);
}
int main() {
long long int n,i,t;
while(t–) {
scanf("%lld\n",&n);
long long int a[n];
for (i=0;i<n;i++) {
scanf("%lld",&a[i]);
}
if (a[0]==0) {
return 0;
}
else {
largestarea(a,n);
}
// if (largestarea(a,n)!=0) {
// printf("%lld\n",largestarea(a,n));
// }
// else {
// return 0;
// }
}
}