Calculate the sum of all the elements of an array - the fastest time, complexity minimum space (own sense of ha ha ha)

/ * The fastest time, complexity minimum space (own sense of ha ha ha) to calculate the sum of all the elements of an array (merge ideas) * /

int getsum(int arr[], int length)
{
    int interval = 1;
    int i;
    
    while( interval<length )
    {
        for( i=0; i+interval<length; i+=2*interval )
        {
            arr[i] = arr[i] + arr[i+interval];
        }
        interval *= 2;
    }
    
    return arr[0];
}
Published 18 original articles · won praise 2 · Views 630

Guess you like

Origin blog.csdn.net/weixin_39618542/article/details/102153101