-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;
}
3
  • 3
    Possible duplicate of What is O(...) and how do I calculate it? Commented Jul 23, 2016 at 18:06
  • @gnat this does answer the question though I'd say it may be a bit complex to someone starting out Commented Jul 24, 2016 at 0:01
  • 1
    Looks like homework; ask your instructor for help. Commented Jul 24, 2016 at 15:34

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.