Solution Review: Big (O) of Nested Loop with Multiplication
This review provides a detailed analysis of how to solve the Big O of the "Nested Loop with Multiplication" challenge.
Solution
Press + to interact
Rust 1.57.0
fn main(){let n = 10;let mut sum = 0;let pie = 3.14;let mut counter = 1;while counter < n {println!("{}", pie);for _j in 0..counter{sum = sum+1;}counter *= 2;}println!("{}", counter);}
Time Complexity
The outer loop here runs times. In the first iteration of the outer loop, the body of the inner loop runs once. In the second iteration, it runs twice, and so on. The number of executions of the body of the inner loop increases in powers of 2. So, if is the number of iterations of the outer loop, the body of the inner loop runs a total of times. This geometric series sums to . The inner loop condition requires that in the last time the inner loop runs, it runs at most times. This requires ...
Access this course and 1400+ top-rated courses and projects.