Count sub-sequences of an array having median lying in that subsequence itself We need to calculate the number of sub-sequences of an array having its median lying in the sub-sequence itself. My code is -   

#include &lt;bits/stdc++.h&gt; 
using namespace std; 


#define MOD 1000000007
#define fori0n for(int i=0;i&lt;n;i++)
#define inputLoop for(int j=0;j&lt;t;j++)

// FAST SCANNING .. 
template&lt;typename T&gt; void scan(T &amp;x) 
{ 
     x = 0; 
     bool neg = 0; 
     register T c = getchar(); 

     if (c == '-') 
     neg = 1, c = getchar(); 

     while ((c &lt; 48) || (c &gt; 57)) 
     c = getchar(); 

     for ( ; c &lt; 48||c &gt; 57 ; c = getchar()); 

     for ( ; c &gt; 47 &amp;&amp; c &lt; 58; c = getchar() ) 
             x= (x &lt;&lt; 3) + ( x &lt;&lt; 1 ) + ( c &amp; 15 ); 

      if (neg) x *= -1; 
}



// FAST PRINTING.. 
template&lt;typename T&gt; void print(T n) 
{ 
     bool neg = 0; 

     if (n &lt; 0) 
     n *= -1, neg = 1; 

     char snum[65]; 
     int i = 0; 
     do
     { 
          snum[i++] = n % 10 + '0'; 
          n /= 10; 
     } 

     while (n); 
     --i; 

     if (neg) 
       putchar('-'); 

     while (i &gt;= 0) 
         putchar(snum[i--]); 

     putchar('\n'); 
} 







float median(vector&lt;int&gt; new_array, int num){
       sort(new_array.begin(), new_array.end());
       float median = (num % 2 != 0) ? (new_array[((num+1)/2)-1]) : (float)(new_array[(num/2)-1] + new_array[num/2]) / 2;
      return median;
}

void subsetsUtil(vector&lt;int&gt;&amp; A, vector&lt;vector&lt;int&gt; &gt;&amp; res, 
            vector&lt;int&gt;&amp; subset, int index) 
{ 
for (int i = index; i &lt; A.size(); i++) { 

    // include the A[i] in subset. 
    subset.push_back(A[i]); 
    res.push_back(subset); 

    // move onto the next element. 
    subsetsUtil(A, res, subset, i + 1); 

    subset.pop_back(); 
} 

return; 
} 


vector&lt;vector&lt;int&gt; &gt; subsets(vector&lt;int&gt;&amp; A) 
{ 
    vector&lt;int&gt; subset; 
    vector&lt;vector&lt;int&gt; &gt; res; 

    // include the null element in the set. 
    //res.push_back(subset); 

    // keeps track of current element in vector A; 
    int index = 0; 
    subsetsUtil(A, res, subset, index); 

   return res; 
} 


int main() 
{ 

//ios_base::sync_with_stdio(false);
//cin.tie(NULL);



int t;
scan(t);
//cin&gt;&gt;t;
inputLoop {
int n;
scan(n);
//cin&gt;&gt;n;

// find the subsets of below vector. 
vector&lt;int&gt; arr; 

int input;
fori0n {
    //cin&gt;&gt;input;
    scan(input);
    arr.push_back(input);
}

vector&lt;vector&lt;int&gt; &gt; res = subsets(arr); 
int goodMedian = 0;
// Print result 
for (int i = 0; i &lt; res.size(); i++) { 



    //cout&lt;&lt;"Sub set : "&lt;&lt;i&lt;&lt;" _ With Size :  "&lt;&lt;res[i].size()&lt;&lt;" == ";


    // if size == 1 or 3 
    if(res[i].size() % 2 != 0) {
        // there will always be a good median 
        //cout&lt;&lt;"GOOD MEDIAN ";
        goodMedian++;
    }
    else if(res[i].size() == 2) {
        if(median(res[i], 2) == res[i][0] || median(res[i], 2) == res[i][1]) {
            //cout&lt;&lt;"GOOD MEDIAN ";
            goodMedian++;
        }
    }
    else if(res[i].size() % 2 == 0) {
        int size = res[i].size();
        if(median(res[i], res[i].size()) == res[i][size / 2] || median(res[i], res[i].size()) == res[i][(size - 1)/2]) {
            //cout&lt;&lt;"GOOD MEDIAN ";
            goodMedian++;
        }
    }



    //for (int j = 0; j &lt; res[i].size(); j++) 
    //  cout &lt;&lt; res[i][j] &lt;&lt; " "; 
    //cout &lt;&lt; endl;

}
      print(goodMedian % MOD);
    }
   return 0; 
} 


Can anyone suggest any better algorithm for this problem ?
Don't #include  &lt;bits/stdc++.h&gt; but explicitly include proper requested headers.
Try to limite uses of using namespace std;, it lead to painfull errors and weird bug.
Don't use preprocessor constants. Instead, use const and constexpr values.
Don't use macro, write directly code or use (inline) (constexpr) function  if you use the same code at much places.
Try to return value instead of taking an input reference.
Avoid old array in favour of std::array.
Don't use register keyword in c++
Don't assign 1 or 0 to a bool, use true and false instead.
getchar return int, why trying to convert to T. 
Try to use C++ iostream instead of getchar or putchar
Instead of magical values, use const char (eg 'A') to express chars bound..
Use curly braces for your statement
Don't try to be smart putting many statement on the same line, separated by comma. It's not.
The expression n *= -1 can by simplified in n = -n
Don't use for loops if you don't want Initialization+Condition+Operation. It's ugly.
Take care about indents.