fork download
  1. #include <bits/stdc++.h>
  2. #define pc(x) putchar_unlocked(x);
  3.  
  4. using namespace std;
  5.  
  6. void writeint (int n);
  7. int scanint(void);
  8.  
  9. int scanint(void)
  10. {
  11. int x;
  12. char c = getchar_unlocked();//this will read the first character.
  13. while(c<'0')
  14. {
  15. c = getchar_unlocked();//if the first character not between 0 & 9 this while loop will work
  16. //untill it get the valid integer between 0 & 9
  17. }
  18. x=0;
  19. while(c>='0' && c<='9')
  20. {
  21. x = (x<<3) + (x<<1) + c - 48;
  22. c = getchar_unlocked();//rest of the characters goes here.
  23. }
  24.  
  25. return x;
  26. }
  27.  
  28.  
  29. void writeint (int n)
  30.  
  31.  
  32.  
  33.  
  34. {
  35.  
  36.  
  37. if(n<10)
  38. {
  39. pc(n + '0');
  40. pc('\n')
  41. return;
  42.  
  43. }
  44.  
  45. int N = n, rev, count = 0;
  46. rev = N;
  47.  
  48.  
  49.  
  50. while ((rev % 10) == 0) { //obtain the count of the number of 0s
  51. count++; rev /= 10;
  52. }
  53.  
  54. rev = 0;
  55.  
  56. while (N != 0) { //store reverse of N in rev
  57. rev = (rev<<3) + (rev<<1) + N % 10;
  58. N /= 10;
  59. }
  60.  
  61. while (rev != 0) {
  62.  
  63. pc(rev % 10 + '0');
  64. rev /= 10;
  65.  
  66. }
  67.  
  68. while (count--)
  69. {
  70.  
  71. pc('0');
  72.  
  73. }
  74. pc('\n');
  75.  
  76. return;
  77. }
  78.  
  79.  
  80.  
  81. int main() {
  82.  
  83. //std::ios_base::sync_with_stdio(false);
  84. //std::cin.tie(NULL);
  85.  
  86. int t,n,k;
  87. //scanf("%d",&t);
  88. t = scanint();
  89. while(t--)
  90. {
  91. //scanf("%d%d",&n,&k);
  92. n = scanint();
  93. k = scanint();
  94.  
  95. // printf("%d %d" , n , k );
  96.  
  97.  
  98. int arr[n];
  99.  
  100.  
  101. for(int i=n;i--;)
  102. //scanf("%d",&arr[i]);
  103. arr[i] = scanint();
  104. if(k==1)
  105. {
  106. putchar_unlocked('0');
  107. putchar_unlocked('\n');
  108. continue;
  109. }
  110.  
  111. sort(arr,arr+n);
  112.  
  113. int diff=INT_MAX;
  114. for(int i=0 ; i<=n-k ;++i)
  115. if(diff>arr[i+k-1]-arr[i])
  116. diff=arr[i+k-1]-arr[i];
  117.  
  118. writeint(diff);
  119.  
  120. //writeint(12300023);
  121. }
  122. return 0;
  123. }
  124.  
Time limit exceeded #stdin #stdout 5s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty