Counting the minimum number of blocks to remove to form a triangular temple PROBLEM STATEMENT : 


  You want to build a temple for snakes. The temple will be built on a
  mountain range, which can be thought of as n blocks, where height of
  i-th block is given by hi. The temple will be made on a consecutive
  section of the blocks and its height should start from 1 and increase
  by exactly 1 each time till some height and then decrease by exactly 1
  each time to height 1, i.e. a consecutive section of 1, 2, 3, .. x-1,
  x, x-1, x-2, .., 1 can correspond to a temple. Also, heights of all
  the blocks other than of the temple should have zero height, so that
  the temple is visible to people who view it from the left side or
  right side.
  
  You want to construct a temple. For that, you can reduce the heights
  of some of the blocks. In a single operation, you can reduce the
  height of a block by 1 unit. Find out minimum number of operations
  required to build a temple.
  
  INPUT
  
  The first line of the input contains an integer T denoting the number
  of test cases. The description of T test cases follows.
  
  The first line of each test case contains an integer n.
  
  The next line contains n integers, where the i-th integer denotes hi
  
  OUTPUT
  
  For each test case, output a new line with an integer corresponding to
  the answer of that testcase.
  
  CONSTRAINTS
  
  1 ≤ T ≤ 10
  
  2 ≤ N ≤ 105
  
  1 ≤ Hi ≤ 10^9
  
  Example Input

3
3
1 2 1
4
1 1 2 1
5
1 2 6 2 1

  
  Output

0
1
3



MY SOLUTION

#include &lt;stdio.h&gt;

void quickSort(int arr[], int low, int high);
int partition (int arr[], int low, int high);
void swap(int* a, int* b);

int main(void) {
    // your code goes here
    int t=0, i=0, n=0, move=0, j=0;
    scanf("%d\n", &amp;t);
    for(i=1;i&lt;=t;++i)
    {
        scanf("%d\n", &amp;n);
        int arr[n];
        int i5;
        for(i5=0;i5&lt;n;++i5)
        scanf("%d ", &amp;arr[i5]);

        quickSort(arr, 0, n-1);

        int max= (arr[n-1]!=arr[n-2])?arr[n-2]+1:arr[n-1];
        move = arr[n-1]-max;
        --max;
        int i3=0;
        for(i3=n-2;i3&gt;0;--max)
        {
            move+=((arr[i3]-max)+(arr[i3-1]-max));
            i3-=2;
            if(max==1)
            {
                break;
            }
        }
        for(j=0;j&lt;=i3;++j)
        {
            move+=arr[j];
        }
        printf("%d\n", move);
    }
    return 0;
}

// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

/* This function takes last element as pivot, places
   the pivot element at its correct position in sorted
    array, and places all smaller (smaller than pivot)
   to left of pivot and all greater elements to right
   of pivot */
int partition (int arr[], int low, int high)
{
    int pivot = arr[high];    // pivot
    int i = (low - 1);  // Index of smaller element
    int j;

    for (j = low; j &lt;= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] &lt;= pivot)
        {
            i++;    // increment index of smaller element
            swap(&amp;arr[i], &amp;arr[j]);
        }
    }
    swap(&amp;arr[i + 1], &amp;arr[high]);
    return (i + 1);
}

/* The main function that implements QuickSort
 arr[] --&gt; Array to be sorted,
  low  --&gt; Starting index,
  high  --&gt; Ending index */
void quickSort(int arr[], int low, int high)
{
    if (low &lt; high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        int pi = partition(arr, low, high);

        // Separately sort elements before
        // partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
Your code is not working for some cases.
Consider for example input 1 1 2 2 4.
The answer should be 6, but your code returns 1.