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;

1 Answer 1


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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.