What is the time complexity of finding the sum of all elements in an N × M…

2023

What is the time complexity of finding the sum of all elements in an N × M two-dimensional array?

  1. A.

    O(N + M)

  2. B.

    O(N × M)

  3. C.

    O(log(N × M))

  4. D.

    O(1)

Attempted by 101 students.

Show answer & explanation

Correct answer: B

The correct answer is Option B: O(N x M). To find the sum of all elements in an N x M two-dimensional array, every single element must be visited exactly once. Since there are N rows and M columns, the total number of elements is N x M, resulting in a linear time complexity relative to the total input size, denoted as O(N x M).

Explore the full course: Btsc Lab Assistant