fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static boolean agg(int[] input,int d,int cows)
  7. {
  8. int f=input[0];
  9. int i=1;
  10. cows=cows-1;;
  11. while(cows>0&&i<input.length-1)
  12. {
  13. if(input[i]-f>=d) {
  14. cows--;
  15. f=input[i];
  16. }
  17. i++;
  18. }
  19. if(cows<=0)
  20. {
  21. return true;
  22. }else {
  23. return false;
  24. }
  25. }
  26.  
  27.  
  28.  
  29.  
  30. public static void main (String[] args) throws java.lang.Exception
  31. {
  32. Scanner sc=new Scanner(System.in);
  33. int t=sc.nextInt();
  34. while(t-->0)
  35. {
  36. int no=sc.nextInt();
  37. int cows=sc.nextInt();
  38. int input[]=new int[no];
  39. for(int i=0;i<no;i++)
  40. {
  41. input[i]=sc.nextInt();
  42. }
  43. Arrays.parallelSort(input);
  44. int l=input[no-1]-input[0];
  45. int dis[] =new int [l];
  46. for(int i=0;i<l;i++)
  47. {
  48. dis[i]=i+1;
  49. }
  50.  
  51.  
  52. int si=0;
  53. int ei=dis.length-1;
  54. int mid=(si+ei)/2;
  55. int ans=-1;
  56. while(si<=ei)
  57. {
  58. mid=(si+ei)/2;
  59.  
  60. int d=dis[mid];
  61. //System.out.println(d);
  62. boolean a=agg(input,d,cows);
  63. if(a)
  64. {
  65. ans=d;
  66. si=mid+1;
  67.  
  68. }else {
  69. ei=mid-1;
  70. }
  71. }
  72.  
  73.  
  74.  
  75. System.out.println(ans);
  76. }
  77. }
  78. }
Runtime error #stdin #stdout #stderr 0.16s 35316KB
stdin
1
2
1
1
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:41)