سؤال

The task is to analyze the following algorithm and calculate its time complexity.

I solved it as taking nested loops are 3 so O(n^3).

How do I solve this problem?

MSS (A[], N)              //Where N is size of array A[]
{
    int temp  = 0, MS  = 0;
    For (int i = 0; i < N; i++)
    {
        for(int j = i; j < N; j++)
        {         
            temp  = 0;
            for(int k = i; k <= j; k++)
                        temp = temp +  A[k];
            if(temp > MS)
                        MS = temp;
        }
    }
    return(MS);
}
هل كانت مفيدة؟

المحلول

Well, you can proceed formally as such:

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top