Given the following program fragment to add two integer arrays A and B, each of size N. The sum is stored in integer array C of the same size.
Given the following program fragment to add two integer arrays A and B, each of size N. The sum is stored in integer array C of the same size.
for (i=1; i<=N; i++)
C[i] = A[i]+B[i];
The running time of this for loop is ______.
Show answerHide answer
Correct answer: (B) O(N)
Explanation
The loop body does constant work and runs N times, so the running time is linear, O(N). O(2N) also simplifies to O(N).