title: Triplet Codility in C - getting only 11 % (Training)
body: <p>I am quite new to C and learning it partly also by going through Codility training. </p>
  
  <p>For the triplet question, however I am only getting 11 % I am not sure what is wrong here. The question is: A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P &lt; Q &lt; R &lt; N).</p>
  
  <p>Your goal is to find the maximal product of any triplet.</p>
  
  <p>Write a function:</p>
  
  <pre><code>int solution(int A[], int N); 
  </code></pre>
  
  <p>that, given a non-empty zero-indexed array A, returns the value of the maximal product of any triplet. 
  For example, given array A such that:</p>
  
  <pre><code>A[0] = -3
  A[1] = 1
  A[2] = 2
  A[3] = -2
  A[4] = 5
  A[5] = 6 
  </code></pre>
  
  <p>the function should return 60, as the product of triplet (2, 4, 5) is maximal.</p>
  
  <p>Assume that: 
  •N is an integer within the range [3..100,000];
   •each element of array A is an integer within the range [−1,000..1,000].</p>
  
  <p>Complexity: 
  •expected worst-case time complexity is O(N*log(N));
   •expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).</p>
  
  <p>My Code which gives me 11 % is, I want to know where I am going wrong with this in this code. I first sort the matrix and then compare the three largest positive numbers and 2 largest negative together with largest positive:</p>
  
  <pre><code>int solution(int A[], int N) {
      int i,j,PQR_pos,PQR_neg, temp;
  
      for (i=0; i&lt;N; i++) { 
          for (j=0; j&lt;N-i; j++) 
              if (A[j+1] &lt; A[j]) { /* compare the two neighbours */ 
                  temp = A[j]; /* swap a[j] and a[j+1] */ 
                  A[j] = A[j+1]; 
                  A[j+1] = temp; 
              } 
      }
  
      PQR_pos = A[N] * A[N-1] * A[N-2];
      PQR_neg = A[N] * A[1] * A[0];
  
      if (PQR_pos&gt;PQR_neg) return PQR_pos;
      else return PQR_neg;
  
  }
  </code></pre>