-1

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);
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 1st you need to do is to tell what operations are you counting. Then count them properly. There will be 3 sums, but it does not mean necessarily its O(n^3). – luk32 May 04 '14 at 18:23
  • I also did some working by searching material regarding complexities and after reading them i solved as follows: first for loops from[1-N] the next for loop has different behavior so it might be log n last for loop is like second for loop so combining all these i have n^2log(n). But i am confused about its accuracy. Kindly share our Knowledge. – Passionate Programmer May 04 '14 at 18:28
  • 1
    @user3602001... Did you read some text about `O notation`? read it again. – someone May 04 '14 at 18:31

1 Answers1

0

Well, you can proceed formally as such:

enter image description here