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?
- A.
O(N + M)
- B.
O(N × M)
- C.
O(log(N × M))
- 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).