title: What is the complexity of this program
body: <p>I have solved a question on HackerEarth.
  The question is</p>
  
  <p>Phineas is Building a castle in his backyard to impress Isabella ( strange, isn't it? ). He has got everything delivered and ready. Even the ground floor has been finished. Now is time to make the upper part. This is where the things become interesting. As Ferb is sleeping in the house after a long day painting the fence (and you folks helped him, didn't ya!), Phineas has to do all the work himself. He is good at this, and all he wants you to do is operate the mini crane to lift the stones. Stones for the wall has been cut and ready waiting for you to lift them up.</p>
  
  <p>Now we don't have Ferb to operate the mini crane, in which he is an expert, we got to do the job as quick as possible. We are given the maximum lifting capacity of the crane, and the weight of each stone. Since it's a mini crane, we cannot place more then 2 stones (of any possible size) at a time, or it will disturb the balance of the crane. we need to find out in how many turns we can deliver the stones to Phineas, who is building the castle.</p>
  
  <p>INPUT: First line of input gives T, the number of test cases. For each test case, first line gives M, the maximum lifting capacity of the crane. first integer N of next line of each test case gives the number of stones, followed by N numbers, specifying the weight of individual stone X.</p>
  
  <p>OUTPUT: For each test case, print the minimum number of turns the crane is operated for all stones to be lifted.</p>
  
  <p>CONSTRAINTS:</p>
  
  <pre><code>1 &lt;= T &lt;= 50
  1 &lt;= M &lt;= 1000
  1 &lt;= N &lt;= 1000
  </code></pre>
  
  <p>Sample Input</p>
  
  <pre><code>1
  50
  3 28 22 48
  </code></pre>
  
  <p>Sample Output</p>
  
  <pre><code>2
  </code></pre>
  
  <p>Explanation
  In first turn, 28 and 22 will be lifted together. In second turn 48 will be lifted.
  Discard the stones with weight > max capacity of crane.</p>
  
  <p>Now I have solved this question and I my source code is</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  
  int main(void) {
      int T = 0;
      scanf("%d",&amp;T);
      while(T--) {
          int i = 0,M = 0, N = 0,max = 0, res = 0, index = 0, j = 0, temp = 0;
          vector&lt;int&gt; v1;
          scanf("%d",&amp;M);
          scanf("%d",&amp;N);
          for(i = 0; i &lt; N ;++i) {
              scanf("%d",&amp;temp);
              if(temp &lt;= M)
                  v1.push_back(temp);
          }
  
          for(i = 0; i &lt; v1.size() ; ++i) {
              max = 0;
              index = 0;
              if(v1[i] != -1) {
                  for(j = i + 1; j &lt; v1.size(); ++j) {
                      if(v1[j] != -1) {
                          temp = v1[i] + v1[j];
                          if(temp &gt; max &amp;&amp; temp &lt;= M) {
                              max = temp;
                              index = j;
                          }
                      }
                  }
                  ++res;
                  v1[i] = -1;
                  v1[index] = -1;
              }
          }
  
          printf("%d\n",res);
      }
  
      return 0;
  
  }
  </code></pre>
  
  <p>Now here are my question</p>
  
  <ol>
  <li>I want to know the average case time complexity of this code. Also I think worst case complexity of this code would be O(N^2).</li>
  <li>This is a brute force approach or dynamic programming approach?</li>
  <li>Is there any better approach then this?</li>
  </ol>