What is problem with my code

You are somewhat following a wrong approach, You need to calculate the max change in a and b differently instead together, Follow like this,
for(int i=0;i<n;i++)
{
change_x=length[i+1]-length[i];
change_x=change_x-1;
if(change_x>max_x)
{
max_x=change_x;
}
}