1 / 6
Jan 2021

Why am I getting a run time error for this?

#include
#include
using namespace std;
bool cowplace(int ar[],int c,int n,int sep)
{
int last=ar[0];
int count=1;
for(int i=1;i<n;i++)
{
if(ar[i]-last>=sep)
{
last=ar[i];
count++;

	if(count==c)
    	return true;
	}
}
return false;

}

int main() {

// your code here
int t;
cin>>t;
while(t--)
{
   
	int n,c;
	cin>>n>>c;
	int ar[10000];
	for(int i=0;i<n;i++)
	       cin>>ar[i];
	int ans=0;
	sort(ar,ar+n);
	int s=0;
	int e=ar[n-1]-ar[0];
	
	while(s<=e)
	{
		int mid=s+(e-s)/2;
    	bool cowskeep=cowplace(ar,c,n,mid);
		if(cowskeep)
		{
			ans=mid;
			s=mid+1;
		}
		else
		{
			e=mid-1;
		}
	}

      cout<<ans;
}
	return 0;

}

  • created

    Jan '21
  • last reply

    Jan '21
  • 5

    replies

  • 543

    views

  • 3

    users

  • 1

    link

You might want to check that.

Either create the array during runtime or initialize it with the max size of the array that can given outside of the main function. Here you are getting runtime error coz you are creating array of size 1e4 for each test case but according the input constraints the max size of N can be 1e5.

Suggested Topics

Want to read more? Browse other topics in Online Judge System or view latest topics.