1
static int sumAfterPos(int[] A) {
  int N = A.length;
  for (int i = 0; i < N; i += 1) {
    if (A[i] > 0) {
      int S;
      S = 0;
      for (int j = i + 1; j < N; j += 1) {
        S += A[j];
      }
      return S;
    }
  }
  return 0;
}

I am having trouble figuring out whether this code runs in O(n^2) or in O(n). I am not sure whether the return S will have a big impact on the runtime.

6
  • 4
    Non-analysis way to find out: just run this thing against a dataset of size 1, 10, 100, 1000, 10000, etc. and simply see how it performs. You'll get a better answer than just O(n) or O(n²) etc. Cheating? not really, it's how you quick-check whether code will behave in the real world. Commented Oct 13, 2014 at 1:26
  • @Mike'Pomax'Kamermans - Cool. Where did you learn this ? Commented Oct 13, 2014 at 1:27
  • 1
    Real world needs to quickly evaluate "real dataset" behaviours of distributed sensor networks. You need to know if an algorithm is going to choke, you find out when it chokes, and plot the input/complexity response. Done. Even if you don't have the source code this gives you the implementation complexity and you can move on with getting the job done by using the algorithm with the best performance. Commented Oct 13, 2014 at 1:29
  • @Mike'Pomax'Kamermans is it safe to say that it runs theta(n)? I am just confused of the manner that the inner for loop will behave. Commented Oct 13, 2014 at 1:31
  • John Smith has you covered there. You're doing an O(n) linear run with a branch on if (A[i] > 0). When that happens you enter a branch that will break your loop, while running through "the rest of your list". This code is O(n) worst case, best case, and average case. Commented Oct 13, 2014 at 1:34

2 Answers 2

3

It is O(N) in time.

For example, A[K] > 0, then you have already have K steps. Then you run another N-K steps and return. So totally you have O(N).

Let us say, all A[i] < 0, this will make the inner loop away. So it is O(N) in this case.

Now let us say, A[0] > 0, this will make the out loop only repeat once and the inner loop will run from 1 to N - 1, so totally you have 1 + (N-1 - 1 + 1) = N.

Now let us say, A[1] > 0, this will make the out loop only repeat twice and the inner loop will run from 2 to N - 1, so totally you have 2 + (N-1 - 2 + 1) = N.

...

Now let us say, A[k] > 0, this will make the out loop only repeat k + 1 times and the inner loop will run from k + 1 to N - 1, so totally you have k + 1 + (N-1 - k -1 + 1) = N.

Now let us say, A[N-1] > 0, this will make the out loop only repeat N and the inner loop will never run, so totally you have N times.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you elavorate more on your reasoning on how you figured out A[k] > 0 will run N-k Steps? (i.e I thought that the outer loop will run O(N) and the inner will run O(N) as well that why I was concluding that the overall run time would have been O(n^2).
Thank you so much for this explanation it is a wonderful!! I have one last question (Sorry for so many questions) How can we determine whether two inner loops will run O(n^2) vs O(n)? This is the origin of my confusion.
Thanks for the help! I thought over it a lot and finally understood what you trying to explain to me.
3

This looks to be O(n), or rather, exactly N iterations will occur. If the statement (A[i] > 0) is true, then a value would be returned after the inner for loop completes its iterations. If there are N elements in the array A, and the outer for loop is at iteration i, and the conditional is met, then the inner for loop will iterate at most N-i times and then return. If the conditional is never met, then the outer for loop will iterate N times. Thus, exactly N iterations between the outer- and inner- for loops will execute.

Return statements are generally never taken into account in determining the runtime complexity of an algorithm (unless the return statement for some reason is non-trivially complex (eg recursion)).

EDIT: For another perspective, note that as the index i increases linearly, the starting point for the index j increases linearly at the same rate, and thus the possible remaining work is decreasing linearly at the same rate as well. Since the the function is guaranteed to return once the inner loop is reached, the total number of iterations between the two for loops is i+j=N by the end of execution.

1 Comment

Thanks for the further explanation of this problem. Over reading everyones explanation I finally understood why the Run Time is O(n).

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.