Collecting magical berries Here is the link of the problem for which I need your help. 

I came up with solution and it is working fine for all cases, but it takes more time. I need your help in reducing the runtime of code. I unable to optimised it more.

#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

long nChoosek( unsigned n, unsigned k )
{
    if (k &gt; n) 
        return 0;
    if (k * 2 &gt; n) 
        k = n-k;
    if (k == 0) 
        return 1;
    long result = n, temp;
    for( int i = 2; i &lt;= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}

int main()
{
    int n,q,i,j,left,right;
    cin&gt;&gt;n;
    int arr[n];
    for(i=0;i&lt;n;i++)
    {
        cin&gt;&gt;arr[i];
    }
    cin&gt;&gt;q;
    string s;
    while(q--)
    {
        int b=0,r,q,re;
        long res=1;
        cin&gt;&gt;s &gt;&gt;left &gt;&gt;right;
        if(s=="change")
        {
            arr[left-1]=right;
        }
        else
        {
            for(i=left-1;i&lt;right;i++)
            {
                b+=arr[i];
            }
            r=(right-left+1);
            q=(b/r);
            re=(b%r);
            res*=nChoosek(r,re);
            for(i=0;i&lt;re;i++)
            {
                res*=nChoosek((b-(i*(q+1))),(q+1));    
            }    
            i=0;
            while((r-re-i) &gt; 1)
            {
                res*=nChoosek(((r-re-i)*q),q);
                i++;
            }
                            res%=3046201;
            cout&lt;&lt;res&lt;&lt;endl;
        }
    }
    return 0;
}


I also tried to replace the function nChoosek

long nChoosek( unsigned n, unsigned k )
{
    if (k &gt; n) 
        return 0;
    if (k * 2 &gt; n) 
        k = n-k;
    if (k == 0) 
        return 1;
    long result = n, temp;
    for( int i = 2; i &lt;= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}


By new recursive function ncr but no change in result:

long ncr(int n,int r)
{
    if((r==0)||(n==r))
        return 1;
    else 
        return (ncr(n-1,r-1)+ncr(n-1,r));
}
No optimisation so far but a few comments to make your code easier to read/improve :

1 Don't do "using namespace std;"

2 Give your variables meaningful names.

3 Declare your variables in the smallest possible scope. Also, you should declare and define in the same times when it's possible. That would make things much easier (such as int res = nChoosek(r,re); instead of res*=nChoosek(r,re);).

4 Be consistent. You use for(i=0;i&lt;re;i++) {foo;} and then, a few lines later i=0; while((r-re-i) &gt; 1){foo; i++;}. Using another for loop would have have been a better option : for (i=0; i&lt;r-re-1; i++) {foo;}

5 Enable the warnings in your compiler. A few weird things such as the useless statement in for(i-0;i&lt;n;i++) should be detected.

6 Add a comment about the algorithm you've used.

Once this is done, we'll be in better conditions to start thinking about optimisations.