title: New Year Chaos HackerRank Practise Problem - C# solution optimization
body: <pre><code>static void minimumBribes(int[] q)
  {
      Int32 TotalCount = 0;
      bool blnSuccess = true;
      Int32[] size = Ordering(q);
      for (int intI = 0; intI &lt; q.Length; intI++)
      {
          Int32 Tempvariable = 0;
          Int32 TooChaotic = 0;
          Int32 index = Index(size,q[intI]);
          do
          {
              if (q[intI] != size[intI])
              {
                  Tempvariable = size[index];
                  size[index] = size[index - 1];
                  size[index - 1] = Tempvariable;
                  index = index - 1;
                  TooChaotic = TooChaotic + 1;
                  if (TooChaotic &gt; 2)
                  {
                      break;
                  }
                  TotalCount = TotalCount + 1;
              }
          } while (q[intI] != size[intI]);
          if (TooChaotic &gt; 2)
          {
              Console.WriteLine("Too chaotic");
              blnSuccess = false;
              break;
          }
      }
      if (blnSuccess)
      {
          Console.WriteLine(TotalCount);
      }
  }
  
  static int[] Ordering(int[] z)
  {
      int[] r = new int[z.Length];
      r = z.OrderBy(x =&gt; x).ToArray();
      return r;
  }
  static int Index(int[] z,int integer)
  {
      for (int intI = 0; intI &lt; z.Length; intI++)
      {
          if(z[intI]== integer)
          {
              return intI;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>This code is working fine, but its runtime is too long.
  I'm getting <em>"Terminated due to timeout"</em> in <em>HackerRank</em>. However, the solution is working fine on the local computer but it's taking more time.
  Problem Link:<a href="https://www.hackerrank.com/challenges/new-year-chaos/problem" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/new-year-chaos/problem</a>.</p>
  
  <p><strong>Sample Input</strong></p>
  
  <p>2 (the number of test cases)</p>
  
  <p>5 (number of people in the queue)</p>
  
  <p>2 1 5 3 4 (n space-separated integers describing the final state of the queue)</p>
  
  <p>5 (number of people in the queue)</p>
  
  <p>2 5 1 3 4 (<em>n</em> space-separated integers describing the final state of the queue).</p>
  
  <p>It must print an integer representing the minimum number of bribes necessary, or Too chaotic if the line configuration is not possible.</p>
  
  <p><strong>Output</strong>
  3</p>
  
  <p>Too chaotic</p>
  
  <p><strong>Question:</strong></p>
  
  <p>How do I reduce its runtime? Presently, I am using an array.</p>