#include<stdio.h>
int main()
{
int arr[1000],n,j,max=0,m,i;
scanf("%d",&i);
while(i–)
{
scanf("%d",&n);
scanf("%d",&max);
arr[0]=max;
for(j=1;j<n;j++)
{
scanf("%d",&arr[j]);
if(max<arr[j])
max=arr[j];
}
m=max;
for(j=0;j<n;j++)
{
// printf("\t%d %d",max,j);
if(max<arr[j])
{
//j=0;
j=-1;
m++;
max=m;
// printf(“uu-%d”,max);
continue;
}
max--;
}
printf("%d\n",m);
max=0;
}
return 0;
}
Please provide link to the problem
1 Like
An also either format your code properly, or just give an ideone link
Hi,
Look initially, you initialized the array with 1000 elements (int arr[1000])
and then when the compiler gets to
for(j=1;j<n;j++)
{
scanf("%d",&arr[j]);
if(max<arr[j]) this statement
max=arr[j];
}
look n value can get up to 10^5 but you have the arr(the array) with the size of 10^3. so it will overflow. so try fixing that I believe it’s the reason you getting runtime error.
Try initializing that array to int arr[100000].
…
1 Like
Pay a closer attention to constraints next time. Index-out-bound exception is giving you runtime error, as kunnu rightly pointed out.
1 Like