AGGRCOW - Aggressive cows - SPOJ Solution C++

  Problem Link : AGGRCOW 


👉 Hint : edit please

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long int

int arr[100001];


bool func(ll d,ll n,ll c)
{
    ll prev=-1;
	if(c>n)
		return 0;
	for(ll i=0;i<n;i++)
	{
		if(i==0 || arr[i]-prev>=d)
		{
		   
			--c;
            prev=arr[i];
			if(c==0)
				return 1;
		}
		
	}
	return 0;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		ll n,c;
		cin>>n>>c;
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr,arr+n);
		ll low=0,high=arr[n-1]-arr[0];
		ll mid;
		while(low<high)
		{
			mid=low+(high-low+1)/2;
			if(func(mid,n,c))
				low=mid;
			else
				high=mid-1;
			
		//	cout<<mid<<" ";	
		}
		cout<<high<<"\n";

	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!