What could be the possible error?

Hi, Please help me to find possible error in the given code.
My Code :
#include
using namespace std;
bool possible(int a[],int n,int c,int dis){
int lc=a[0];
c–;
for(int i=1;i<n;i++){
if( a[i]-lc>=dis ){
lc=a[i];
c–;
if(c==0) return true;
}
}
return false;
}

int func(int a[],int n,int c){
int l=0;
int r=a[n-1]-a[0];
int mid,ans=0;
while(l<=r){
mid=(l+r)/2;

	if(possible(a,n,c,mid)){
		//cout<<"possible";
		ans=mid;
		l=mid+1;
	}
	else r=mid-1; 
}
return ans;

}

int main() {
int n,c;
cin>>n>>c;
int a[n+1];
for(int i=0;i<n;i++) cin>>a[i];

cout<<func(a,n,c)<<endl;
return 0;

}

please share code in cb ide


updates are required in code
added comments

data type needs to be long long int since constraints re large

Thanks. Main reason was we have to sort the array first thanks.

1 Like