Which statement is true for this piece of code?

I am preparing for the exam, and for this I make some costs - when I come to the question, I'm a little unsure.

Question:

enter image description here

I am sure that the answer cannot be C or D, because the best run time for the code is O (1) and the worst run time is O (n).

I also think that B should be the correct answer, as the if-statement inside the forloop compares if A [i] == 0. What is the worst case of N.

What I'm not sure about when you call something “array access” and when is this a comparison? That is why I am not sure if the answer is B or A.

+4
source share
2 answers

It seems to me that you are right.

  • : ==, !=, >, <, >=, <=, .
  • : arr[i], . arr[i] = x, print(arr[i]) ..

.

  • ? - .
  • ? ( N , N).

? ? ops .

?

+3

: B - 2N . , . N (i < n). if - N 2N .

C, " " , 0, 2 , O (1) .

, , D; . .

A, N . s[i], 0.

:

int n = s.length();
for (int i=0; i < n; i++)
{
    int v = s[i];
    if (v == 0)
        return i;
}

, , . N .

+5

All Articles