Question 29

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 ______.
  1. AO(1)
  2. BO(N)
  3. CO(2N)
  4. DO(N²)
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).

From DSSSB TGT Computer Science Practice Set 1

This is question 29 of DSSSB TGT Computer Science Practice Set 1. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.