I am trying to do these online competitions where you get the given exercise and are supposed to upload the source file.The site is not in English so I will try to summarize the exercise.The first line of input states P, the number of problems ( 1 <= P <= 100 ). Subsequently, there are two lines for each problem. The first contains numbers X and Y ( 1 <= X, Y <= 100000)  X being number of elements and Y being the difference I am looking for. On the next line there are X numbers (0 <= N <= 100000) divided by a space. For every problem, I am supposed to print the number of pairs that differ by Y.As an example input :25 21 5 3 4 25 46 3 6 2 2Example output:34I wrote a simple programme that did the thing but when I uploaded the source file I got time limit exceeded error. After that I discovered that my program is unable to handle big input. I tried to move on to the next exercise but I discovered almost all of them have big inputs. I tried to improve it myself, then I tried to google it, but sadly I still cannot get past the error. This is the code I ended up with :#include <stdio.h>#include <stdlib.h>#include <string.h>#define SIZE 80000int compare(const void *a, const void *b);int main(int argc, char const *argv[]) {  char buffer[SIZE];  unsigned int numberOfGames, numberOfStudents, wantedDifference;  unsigned int numberOfDifferentPairs;  unsigned int counter;  size_t i, j;  scanf(%u, &numberOfGames);  while (numberOfGames--) {    scanf(%u %u, &numberOfStudents, &wantedDifference);    scanf(\n);    unsigned long long int *studentsHeights =        (unsigned long long int *)malloc(sizeof(unsigned long long int) * numberOfStudents);    counter = 0;    numberOfDifferentPairs = 0;    studentsHeights[counter] = 0;    while (counter < numberOfStudents) {      fgets(buffer, SIZE, stdin);      size_t stringSize = strlen(buffer);      for (i = 0; i < stringSize; i++) {        if (buffer[i] == ' ' || buffer[i] == EOF || buffer[i] == '\n') {          counter++;          if (counter < numberOfStudents)            studentsHeights[counter] = 0;        } else {          studentsHeights[counter] =              studentsHeights[counter] * 10 + (buffer[i] - '0');        }      }    }    qsort(studentsHeights, numberOfStudents, sizeof(unsigned int), compare);    for (i = 0; i < numberOfStudents - 1; i++) {      unsigned int lastAddition;      if (studentsHeights[i] == studentsHeights[i - 1]) {        numberOfDifferentPairs += lastAddition;        continue;      } else {        lastAddition = 0;      }      for (j = i + 1; j < numberOfStudents; j++) {        if ((studentsHeights[j] - studentsHeights[i]) == wantedDifference) {          numberOfDifferentPairs++;          lastAddition++;        }      }    }    printf(%u\n, numberOfDifferentPairs);  }  return 0;}int compare(const void *a, const void *b) {  if (*(unsigned int *)a < *(unsigned int *)b)    return -1;  else if (*(unsigned int *)a == *(unsigned int *)b)    return 0;  else    return 1;}I know I am probably doing some trivial mistake but if you could point it out for me I would be really grateful.There are the variables I used:P = numberOfGamesX = numberOfStudentsY = wantedDifferenceN = studentsHeights
Counting pairs of numbers that differ by a given amount
c;programming challenge;time limit exceeded
BugWhen I ran your program, it didn't work.  It turns out you have a problem here:qsort(studentsHeights, numberOfStudents, sizeof(unsigned int), compare);The type of the studentsHeights array is unsigned long long, but you are sorting as if it were an array of unsigned int.  This causes the sort to not work properly.Input parsing too complicatedYou have a custom integer parser that is totally unnecessary.  Perhaps you thought it would make your program faster, but input parsing isn't what is making your program too slow.Solution is accurate but too slowYour solution works but it takes \$O(n^2)\$ time because of your nested loop.  One easy way to make your program \$O(n \log n)\$ would be to change your j loop to binary search for the start of the desired height and then count the number of duplicates of that height.  This would most likely make your program run within the time limit.  But there is actually a faster and simpler way as well.\$O(n)\$ solution possibleInstead of sorting and counting duplicates like you are doing now, you can take advantage of the fact that the input is limited to numbers in the range 0..100000.  You can set up an array to hold the count of each number and then iterate across the array of counts.  Here is how you would do it:#include <stdio.h>#include <string.h>#define MAX_HEIGHT        100000unsigned long heightCounts[MAX_HEIGHT + 1];int main(void){    int numTestcases = 0;    scanf(%d, &numTestcases);    while (numTestcases--) {        unsigned long numHeights = 0;        unsigned long difference = 0;        unsigned long i          = 0;        unsigned long total      = 0;        scanf(%lu %lu, &numHeights, &difference);        memset(heightCounts, 0, sizeof(heightCounts));        for (i = 0; i < numHeights; i++) {            unsigned long height;            scanf(%lu, &height);            heightCounts[height]++;        }        for (i = 0; i <= MAX_HEIGHT - difference; i++)            total += heightCounts[i] * heightCounts[i+difference];        printf(%lu\n, total);    }}Note: I use unsigned long because int is only guaranteed to be 16 bits.  The problem requires values that are 32 bits long.  The maximum answer given the problem constraints is 2500000000 which fits in a 32 bit unsigned value.