-1
#include <stdio.h>

int F(int L[], int p, int q) {
    if (p < q) {
        int r, f1, f2;
        r = (p + q) / 2;
        f1 = 2 * F(L, p, r);
        f2 = 2 * F(L, r + 1, q);
        return f1 + f2;
    } else if (p == q) {
        return L[p] * L[p];
    }else{
        return 0;
    }
}

int main(void) {
    int arr[8] = {1,2,3,4,5,6,7};
    printf("%d", F(arr, 0, 7));
}

Someone said the time complexity of this code is O(n).

I don't understand it at all...

Isn't it O(logN) ???

ks1322
  • 33,961
  • 14
  • 109
  • 164
Juno Park
  • 3
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 02 '23 at 18:50
  • Hint: Start by setting `p` to zero and then see what happens as `q` increases – Support Ukraine Jan 02 '23 at 18:52
  • 3
    We are here to help, but not that way. You are supposed to show your reasoning about why you think it is O(log N) and we could then validate or invalidate the reasoning. As is, without even explanation about what that code is, and no comments, you are just resquesting us to do a calculation for you. – chrslg Jan 02 '23 at 18:53
  • 1
    One criteria I can give without knowing what your code is: do you think that all values of the array have an influence on the result? Or are there some whose value are not important? Because if the final result use all values, then, there is no way cost can be less than O(n). If you know that some values aren't even read (like for dichotomy search in a sorted list), then, it might be less than O(n) (but could also not be. Not using all values is a necessary condition, not a sufficient one to be less than O(n)) – chrslg Jan 02 '23 at 18:57
  • 1
    Hint: "Someone" is correct.... – Support Ukraine Jan 02 '23 at 18:59
  • I'm sorry. I didn't know because it was my first time asking. Next time, I will pay attention to how to ask questions. Thank you for your good reply. – Juno Park Jan 02 '23 at 19:06
  • Could you show how you calculated `O(logN)`? At first glance it does seem to be `O(N)` – greybrunix Jan 02 '23 at 19:12
  • 3
    "Next time, I will pay attention to how to ask questions." -- you can _edit_ your question, there is no need to wait until next time. – Employed Russian Jan 02 '23 at 19:34
  • The big-O runtime for a recursive function is equivalent to the number of recursive function calls. The value varies with the complexity of the algorithm of the recursive function. Thus, a recursive function of input N that is called N times will have a runtime of O(N). – ryyker Jan 02 '23 at 19:54
  • [Examples of algorithms](https://stackoverflow.com/questions/1592649/examples-of-algorithms-which-has-o1-on-log-n-and-olog-n-complexities) paired with their Big O notation. (Be sure to give an up-click if you find that answer/question useful.) – ryyker Jan 02 '23 at 19:58
  • @juno Park, Technicality. unless there is something to vary the execution time, this code of O(1). Show something that changes like ask the using for the length of the array to process, some `n` or ask for the run-time of `F(arr, 0, n)`. – chux - Reinstate Monica Jan 02 '23 at 20:29
  • BTW, the two `else` are not needed. Code runs the same if removed. Code risks UB with overflow in `L[p] * L[p]` making it O(?), but I suppose we can set aside that concern for now. – chux - Reinstate Monica Jan 02 '23 at 20:32

1 Answers1

0

Answer: The Big-O complexity is O(N)

Explanation:

The program takes a range of some size (i.e. q - p + 1) and cut that range into 2 half. Then it calls the function recursively on these two half ranges.

That process continues until the range has size 1 (i.e. p == q). Then there is no more recursion.

Example: Consider a start range with size 8 (e.g. p=0, q=7) then you will get

1 call with range size 8
2 calls with range size 4
4 calls with range size 2
8 calls with range size 1

So 7 calls (i.e. 1+2+4) with range size greater than 1 and 8 calls with range size equal to 1. A total of 15 calls which is nearly 2 times the starting range size.

So for a range size being a power of 2, you can generalize to be

Number of calls with range size greater than 1: 

    1+2+4+8+16+...+ rangesize/2 = rangesize - 1

Number of calls with range size equal to 1: 

    rangesize

So there will be exactly 2 * rangesize - 1 function calls when range size is a power of 2.

That is Big-O complexity O(N).

Want to try it out?

#include <stdio.h>

unsigned total_calls = 0;
unsigned calls_with_range_size_greater_than_one = 0;
unsigned calls_with_range_size_equal_one = 0;

int F(int L[], int p, int q) {
    ++total_calls;
    if (p < q) {
        ++calls_with_range_size_greater_than_one;
        int r, f1, f2;
        r = (p + q) / 2;
        f1 = 2 * F(L, p, r);
        f2 = 2 * F(L, r + 1, q);
        return f1 + f2;
    } else if (p == q) {
        ++calls_with_range_size_equal_one;
        return L[p] * L[p];
    }else{
        return 0;
    }
}

int arr[200] = {1,2,3,4,5,6,7};

int main(void) {
    
    for (int i=3; i < 128; i = i + i + 1)
    {
        total_calls=0;
        calls_with_range_size_greater_than_one=0;
        calls_with_range_size_equal_one=0;
        F(arr, 0, i);
        printf("Start range size: %3d -> total_calls: %3u calls_with_range_size_greater_than_one: %3u calls_with_range_size_equal_one: %3u\n", i+1, total_calls, calls_with_range_size_greater_than_one, calls_with_range_size_equal_one);
    }
    return 0;
}

Output:

Start range size:   4 -> total_calls:   7 calls_with_range_size_greater_than_one:   3 calls_with_range_size_equal_one:   4
Start range size:   8 -> total_calls:  15 calls_with_range_size_greater_than_one:   7 calls_with_range_size_equal_one:   8
Start range size:  16 -> total_calls:  31 calls_with_range_size_greater_than_one:  15 calls_with_range_size_equal_one:  16
Start range size:  32 -> total_calls:  63 calls_with_range_size_greater_than_one:  31 calls_with_range_size_equal_one:  32
Start range size:  64 -> total_calls: 127 calls_with_range_size_greater_than_one:  63 calls_with_range_size_equal_one:  64
Start range size: 128 -> total_calls: 255 calls_with_range_size_greater_than_one: 127 calls_with_range_size_equal_one: 128
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63