Question

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);
}
Was it helpful?

Solution

Well, you can proceed formally as such:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top