What is the complexity of this loop? I can’t wrap my head around it.
for (i = 0; i < n; ++i) {
for (j = i; j < n; ++j) {
for (k = 0; k < j; ++k) {
// Do something
}
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
O(n^3), I believe. See Square pyramidal number.iloop hasniterations.jloop: (1 + 2 + … + n), starting withniterations, and finishing with1.kloop: (1² + 2² + … n²),jtimes per each iteration of thejloop.And finally: