Today,there was a coding round for internship in our collage.I wrote a code which was giving different output for same test case on ideone and Hackerrank.On ideone output was 4 and on hackerrank output
was 0.I don’t why??can anyone explain.
here is that test case and my code …
4 1
2
3
5
7
#include <iostream>
using namespace std;
int main() {
int s,g;
cin>>s>>g;
int arr[s];
int c=g;
for(int i=0;i<s;i++)
cin>>arr[i];
int len=1;
int val=0;
for(int i=1;i<s;i++)
{
if(arr[i]==arr[i-1]+1)
len++;
else if(arr[i]-arr[i-1]<=g+1)
{
len+=(arr[i]-arr[i-1]);
g=g-(arr[i]-arr[i-1]);
}
else
{
if(val<len)
val=len;
len=1;
//g=c;
}
}
if(val<len)
val=len;
cout<<val<<endl;
return 0;
}