"Max Min" in HackerRank I am trying to implement the solution for the problem "Max Min" in HackerRank. The solution runs for some test cases but for few test cases it pops out showing "Terminated due to Time Out"


  Problem:
  
  Given a list of \$N\$ integers, your task is to select \$K\$ integers
  from the list such that its unfairness is minimized.
  
  Unfairness = Max{K integer Set} - Min{K integer Set}
  
  where max denotes the largest integer among the elements of \$K\$, and
  min denotes the smallest integer among the elements of \$K\$.
  
  Input Format
  
  
  The first line contains an integer \$N\$.
  The second line contains an integer \$K\$.
  \$N\$ lines follow. Each line contains an integer that belongs to the list \$N\$.
  
  
  Note: Integers in the list \$N\$ may not be unique.
  
  Output Format
  
  An integer that denotes the minimum possible value of unfairness.
  
  Constraints 
  
  
  \$2 \le N \le 10^5\$
  \$2 \le K \le N\$
  \$0 \le \text{integer in} N \le 10^9\$
  
  
  Sample Input #00

7
3
10
100
300
200
1000
20
30

  
  Sample Output #00

20



Can anyone please point to me which part of my code is taking longer to implement and what a better solution can be?

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;limits.h&gt;

#define MAX 100000
#define MAX_VAL 1000000001

int list[MAX];

int main() {

    int N, K;
    int i;
    int unfairness=0;
    scanf_s("%d %d", &amp;N, &amp;K);
    int set = K - 1;
    for (i = 0; i &lt; N; i++)
        scanf_s("%d", &amp;list[i]);
    for (int l = 0; l &lt; N - 1; l++){
        for (int j = 0; j &lt; N - l - 1; j++)
        {
            if (list[j] &gt; list[j + 1])
            {
                int temp;
                temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }
    for (int x = 0; x &lt; N - set; x++)
    {
        int differ = list[x + set] - list[x];
        //printf("Dif - %d\n", differ);
        //printf("Unfair - %d\n", unfairness);
        if (unfairness == 0){
            unfairness = differ;
        }
        else if (differ&lt;unfairness)
            unfairness = differ;
    }
    printf("%d", unfairness);
    return 0;
}
The slowness comes from the sort algorithm you've chosen. It's \$O(N^2)\$ and you can do better by using a faster sort algorithm,
such as merge sort or quicksort.
Once you do that, the solution will pass with flying colors.



You could simplify the loop that checks the maximum difference by initializing unfairness to the diff of the first interval:

int unfairness = list[set] - list[0];


And then start the loop from 1, and simplify:

for (int x = 1; x &lt; N - set; x++) {
    int diff = list[x + set] - list[x];
    if (diff &lt; unfairness) {
        unfairness = diff;
    }
}


Also, it would be good to put this code in a separate function.
It's good to have many short functions that do one thing,
than one long function that does many things.



Instead of initializing i at the top of the function,
it would be better to do that inside the for-loop where you use it,
just like you did for other loops.

I recommend to avoid using l as a variable name,
because on some displays and fonts it's too hard to distinguish from 1 or I.