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:

  1. A.

    O(1)

  2. B.

    O(N)

  3. C.

    O(2N)

  4. 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).

Explore the full course: Up Lt Grade Assistant Teacher 2025