Plz correct my solution

@abhay_091 Bro, mid tumhara ek possible answer tabhi ho skta hai na jb saari cows place ho gyi ho?

Or secondly tumne max variable maintain hi nahin kia, cows place to ek se jyada answer ke liye ho jaengi lekin max kya hoga, you have to maintain that.

Here check your correct code, and if your doubt is cleared resolve and rate it.

import java.util.*;
import java.util.Scanner;

class Main
{

public static void main(String[] args)
{
	Scanner sc=new Scanner(System.in);
	int s;
	int c;
	int maxAns = Integer.MIN_VALUE;
	s=sc.nextInt();
	c=sc.nextInt();
	int a[]=new int[s];
	for(int i=0;i<s;i++)
	{
		a[i]=sc.nextInt();
	}
	Arrays.sort(a);
	int start=a[0];
	int end=a[a.length-1];
	int q=0;
	while(start<=end)
	{
		
		int mid=start + (end-start)/2;
		int p=c-1;
		int g=a[0];
		for(int i=1;i<s;i++)
		{
			if((a[i]-g>=mid) && (p>0))
			{
				p--;
				g=a[i];
			}
		}
			 if(p<=0)
			{
				start=mid+1;
				// mid will be one of the answers when all cows are placed.
				q=mid;

				// maintain a max variable as asked in the question.
				maxAns = Math.max(maxAns, q);
			}
			else 
			{
				end=mid-1;
			}
	}
	System.out.println(maxAns);
}

}

Here it is your corrected accepted code.

can u send me the link of your code

@abhay_091 bro upar jo code hai wo accepted hai tumhare hi code ko modify kia hai

without maintaining max we can do it

@abhay_091 Haan bro you can do it, because when the cows are placed for a value x, then you are searching on the right.

But problem in your code is you were initializing q = mid for every case, but q as a current answer will be initialized when you have placed all the cows, see that line.

I am talking bout this, you can remove max.

Understood or not?

Ek possible answer tabhi hoga na jb all cows are placed! but You are always updating q. And check your mid calculating value it should be int mid=start + (end-start)/2;

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.