title: Optimizing the following hackerrank program
body: <p>I want to optimize the following code I made so that it passes all the test cases in a hackerrank problem. It works at the moment but fails some cases due to timeout. Can you guide me as to how to optimize the code without changing the concept?</p>
  
  <p><strong>QUESTION</strong></p>
  
  <blockquote>
    <p>Alice is playing an arcade game and wants to climb to the top of the leaderboard. 
    Can you help her track her ranking as she beats each level? The game uses Dense Ranking, so its leaderboard works like this:</p>
    
    <p>• The player with the highest score is ranked number on the leaderboard.</p>
    
    <p>• Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.</p>
    
    <p>For example, four players have the scores 100, 90, 90, and 80. Those players will have ranks 1, 2, 2 and 3 respectively.</p>
    
    <p>When Alice starts playing, there are already <code>n</code> people on the leaderboard. Alice plays for <code>m</code> levels, and we denote her total score after passing each level.After completing each level, Alice wants to know her current rank.</p>
    
    <p>You are given an array of monotonically decreasing leaderboard scores, and another array of Alice's cumulative scores for each level of the game. You must print  integers: the integer should indicate the current rank of Alice after passing each level.</p>
    
    <p><strong>Input Format:</strong></p>
    
    <ul>
    <li>The first line contains an integer <code>n</code>, denoting the number of players already on the leaderboard. </li>
    <li>The next line contains <code>n</code> space-separated integers describing the respective values of their scores. </li>
    <li>The next line contains an integer <code>m</code>, denoting the number of levels Alice beats. </li>
    <li>The last line contains <code>m</code> space-separated integers describing the respective values of each player.</li>
    </ul>
    
    <p><strong>Output Format:</strong></p>
    
    <p>Print  integers. The  integer should indicate the rank of Alice after passing each  level.</p>
  </blockquote>
  
  <p><strong>INPUT</strong></p>
  
  <pre><code>7
  100 100 50 40 40 20 10
  4
  5 25 50 120
  </code></pre>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>6
  4
  2
  1
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct player {
      int rank;
      int score;
  };
  
  struct node {
      struct player data;
      struct node *next;
  };
  
  int main() {
      int n, i, m, temp, count = 1, x;
      struct node *ptr, *head;
      head = (struct node *)malloc(sizeof(struct node));
      ptr = head;
      scanf("%d", &amp;n);
      for (i = 0; i &lt; n; i++) {
          scanf("%d", &amp;x);
          ptr-&gt;data.score = x;
          if (x &lt; temp)
              count++;
          ptr-&gt;data.rank = count;
          temp = ptr-&gt;data.score;
          ptr-&gt;next = (struct node *)malloc(sizeof(struct node));
          ptr = ptr-&gt;next;
      }
      ptr = NULL;
      ptr = head;
      scanf("%d", &amp;m);
      for (i = 0; i &lt; m; i++) {
          scanf(" %d", &amp;x);
          while (x &lt; ptr-&gt;data.score)
              ptr = ptr-&gt;next;
          if (ptr-&gt;next == NULL)
              ptr-&gt;data.rank = count + 1;
          printf("%d\n", ptr-&gt;data.rank);
          ptr = head;
      }
      return 0;
  }
  </code></pre>
  
  <p>The problem lies in the fact that I am traversing the linked list from the beginning each time I am taking out the rank for Alice. What might be a possible optimization for this?</p>