Given the following program fragment to add two integer arrays A and B, each…
2022
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:
- A.
O(1)
- B.
O(N)
- C.
O(2N)
- D.
O(N)2
Attempted by 1816 students.
Show answer & explanation
Correct answer: B
The loop runs from i = 1 to i = N, executing a constant-time operation inside the loop for each iteration. Hence, the loop executes N times, giving a time complexity of O(N).