Time complexity of recursive function question

Hi there! I just had a quick question regarding the time complexity of this recursive function I implemented.

My function very simply adds up "point values" of all consecutive numbers up to n, with odd numbers being worth 1 "point" and even numbers being worth 2.

Ex: If n is 4, totalSum would be 1+2+1+2 = 6.

I was confused as to if my recursive code below would run at a time complexity of O(n) or O(log(n)). Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int TestSum(int n, int totalSum) {
    
    if(n>0) {
        if(n % 2 == 0) {
            totalSum += 2;
            return TestSum(n-1, totalSum);
        }

        else{
            totalSum += 1;
            return TestSum(n-1, totalSum);
        }
    }
    
    return totalSum;
        
}
Last edited on
It runs in O(n) time.
Topic archived. No new replies allowed.