-1

What will be the result of the time complexity of this piece of code i.e.

int sum(int A[], int n) 
{
    int sum = 0, i;
    for(i = 0; i < n; i++) {
        sum = sum + A[i];  
    }      
    return sum;
}
candied_orange
  • 102,279
  • 24
  • 197
  • 315
raphael
  • 11
  • 1
  • 3
    Possible duplicate of [What is O(...) and how do I calculate it?](http://programmers.stackexchange.com/questions/132331/what-is-o-and-how-do-i-calculate-it) – gnat Jul 23 '16 at 18:06
  • @gnat this does answer the question though I'd say it may be a bit complex to someone starting out – daven11 Jul 24 '16 at 00:01
  • 1
    Looks like homework; ask your instructor for help. – Chris Cirefice Jul 24 '16 at 15:34

1 Answers1

0

A way to start on figuring out if the complexity is O(1) or O(N) or a higher order is to figure out what happens if the value of n goes from n to n+1. If it's O(1) then the run time for the change from n to n+1 will be the same, if it's O(N) then the run time will change from n*k seconds to (n+1)*k seconds (for some constant k). if the change is something else then it's not O(1) or O(N) and some more analysis is required.

This should give you enough information to figure this out, if not then ask away.

daven11
  • 769
  • 5
  • 11