Big O
)}

Problem

This function prints the diagonal elements in a 2D array. What is the time complexity?

def print_diagonal_elements(matrix): for i in range(len(matrix)): for j in range(len(matrix[i])): if i == j: print(matrix[i][j])

Solution

The outer loop takes O(N)O(N) time and the inner loop takes O(C)O(C) time, where CC is the number of columns. We wish to only consider the worst case where the number of columns equals the number of rows. The resulting time complexity will be O(N2)O(N^2).


This workbook was created by Jad and Rayan Slim. Feel free to explore some of their courses:
The Complete Java Development Bootcamp
The Complete Spring Boot Development Bootcamp – Become a Java Web Developer

Feedback Summary
0.0
0 students
5

0%
4

0%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.