How to calculate time complexity for below function? - algorithm

What is the time complexity for the below function?
void function(int n){
for(int i=0; i<n; i++){. //n times
for(int j=i ; j< i*i; j++){ // n*n times
if(j%i == 0){
for(int k=0; k<j; k++){ // j times = n * n times.
++counter;
}
}
}
}
}
Book says O(n^5). But I couldn't understand why book didn't take j%i into account. The k loop determines based on that. Can you clarify?

Let's consider the part of code in which u have doubt :
for(int j=i ; j< i*i; j++)
{
if(j%i == 0)
{
for(int p=0; p<j; p++){
++counter;
}
}
Let's just analyse this part of code .
For every i, j%i==0 will be true whenever j is a multiple of i, i.e.
i, 2i, 3i, .... i*i // upto i*i only,since j<i*i
Now, for every j, Inside complexity is O(j).
So Complexity for this part of code is :
So, Overall Complexity = O(n*n^3) = O(n^4)

In fact the function runs in time O(n^4), since the if condition happens only once every approximately n loops. See below for precise analysis.
void function (int n) {
for(int i=0; i<n; i++) {
// Runs a total of n times
for(int j=i; j< i*i; j++) {
// Runs a total of (n^3 - n)/3 = O(n^3) times
if(j%i == 0) {
// Runs a total of (n^2 - n)/2 = O(n^2) times
for(int k=0; k<j; k++) {
// Runs a total of (3n^4 - 10n^3 + 9n^2 - 2n)/24 = O(n^4) times
++counter;
}
}
}
}
}

Related

Having some trouble with solving for runtime

In Big-Θ notation, analyze the running time of the following three pieces of code/pseudo-code, describing it as a function of the input, n.
1.
void f1(int n)
{
int t = sqrt(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
// do something O(1)
}
n -= t;
}
}
2.
Assume A is an array of size n+1.
void f2(int* A, int n)
{
for(int i=1; i <= n; i++){
for(int k=1; k <= n; k++){
if( A[k] == i){
for(int m=1; m <= n; m=m+m){
// do something that takes O(1) time
// Assume the contents of the A[] array are not changed
}
}
}
}
}
3.
void f3(int* A, int n)
{
if(n <= 1) return;
else {
f3(A, n-2);
// do something that takes O(1) time
f3(A, n-2);
}
}
Any help would be appreciated.

Running Time of Nested Loops, When inner loop false,

01.) Considering the growth time, When the inner loop of a nested loop is false, is its growth time O (n)?
ex:
for (int i=0; i<n; i++){
for (int j=0; j>n; i++){
//some code
}
}
02.) Considering the growth time, When the inner loop of a nested loop has another array length variable as 'm', is its growth time O (n m)?
ex:
for (int i=0; i<n; i++){
for (int j=0; j<m; i++){
//some code
}
}
What is the running time of the following code? (please explain with steps)
for (int i = 0; i < n; i++) {
for (int j = 0; j > n; j++) {
for (int k = 0; k > n; k++) {
System.out.println("*");
}
}
}
Thank You.
Yes, if it is absolutely clear that inner loop will not be executed due to the conditional's being false regardless of the input size, then time complexity will be O(n).
Yes, a detailed analysis can be found below. However, I assume you incorrectly incremented i in your inner loop.
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){ // j++
//some code
}
}
number of operations performed by inner loop in each iteration of outer loop : m
number of operations performed by outer loop : n
Total number of operations : n*m
Time complexity f(n) ∈ O(n*m)
If the code you posted is not incorrect, then the time complexity analyis:
for (int i=0; i<n; i++){
for (int j=0; j<m; i++){
//some code
}
}
j=0 --> j<m (i++)
If 0<m, then it is an infinite loop. It's pointless to make a time complexity analysis.
If 0>=m, then inner loop is not going to be executed, therefore time complexity will be O(n).
Detailed analysis:
for (int i = 0; i < n; i++) { // n many times
for (int j = 0; j > n; j++) { // n many times
for (int k = 0; k > n; k++) { // n many times
System.out.println("*");
}
}
}
All of the loops are iterated n many times. Most-inner loop will perform n operations. Outer loop will perform n, which makes n*n. And most-outer loop will perform n operations, which makes n*n*n in total.
Time complexity f(n) ∈ O(n^3)

What is the Big O notation of a loop in loop

I have this code and cannot understand the Big-O of this... Thanks
for(i = 0; i<n; i++){
for(j = i; j<n; j++){
if (arr[j]%2!=0){
if (minodd > arr[j]){
}
}
}
}
One of the best ways to approach this problem is to break it down into smaller parts.
First, lets look at your inner loop:
for(j = i; j<n; j++){
if (arr[j]%2!=0){ // O(1)
if (minodd > arr[j]){ // O(1)
}
}
}
The if-statements are O(1) or constant time so we can ignore those and we get just the inner for loop:
for(j = i; j<n; j++){
... // O(1) + O(1)
}
Since the worst case scenario is it loops n times we have O(n) + O(1) + O(1) which can be simplified to O(n) which is called linear time.
Next, lets zoom out and replace the inner loop with our new info:
for(i = 0; i<n; i++){
for(j = i; j<n; j++){
if (arr[j]%2!=0){
if (minodd > arr[j]){
}
}
}
}
becomes:
for(i = 0; i<n; i++){
O(n)
}
Since we know the outside for loop will cycle n times in the worst case, and the inside for loop will cycle n times in the worst case: We get O(n x n) or O(n²) which is also know as polynomial time.
Doesn't this just go on for forever?
You have i < n in your inner loop, so I think it's O(inf).
Now that you've updated the loop, I think #e2-e4 is right:
#include <stdio.h>
int eqn(int n)
{
return n > 0 ? n + eqn(n - 1) : 0;
}
int main(int argc, char **argv)
{
int i, j, n, v, a;
v = 0;
n = 5;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
v++;
}
}
// v = 15 ? 15
printf("v = %d ? %d\n", v, eqn(n));
return 0;
}

Why are the Big O Notations for the following codes like this?

Question 1.
My Answer: 1 + n^2 * n = n^3
Correct Answer: O(n)
void f(int n) {
if (n<1000000)
return 0;
for (int i=0; i<(n*n); i++)
return f(n-1);
}
Question 2.
My Answer: n * n * logn = n^2*logn
Correct Answer: O(n^3)
int f(int n) {
for (int i=1; i< (n/2); i++)
for(double j =0; j <100; j+= (100.0/n)
doSomething (n/2); //doSomething(m) has time complexity of O(m)
return 0;
}
Question 3.
My Answer: 1 + n * (logn + 1) = O(nlogn)
Correct Answer: O(logn)
int f(n) {
if (n<1) return;
g(n-1);
}
void g(n) {
f(n/2)
doOne(); // doOne has time complexity O(1)
}
Question 1
void f(int n) {
if (n<1000000)
return 0;
for (int i=0; i<(n*n); i++)
return f(n-1);
}
The for loop is not looping at all because the content is a return statement, so you have at most one loop iteration. This means you can can simplify this code to:
void f(int n) {
if (n<=0)
return 0;
return f(n-1);
}
(simplified in regard for the O(n) analysis)
Here you see why it is O(n) because it is counting down until it hit the recursion stop condition. The fact that there is a "high" value check for n<100000 doesn't matter when you call it with something like f(5*10^300);
Question 2
int f(int n) {
for (int i=1; i< (n/2); i++)
for(double j =0; j <100; j+= (100.0/n)
doSomething (n/2); //doSomething(m) has time complexity of O(m)
return 0;
}
In regard for the O(n) analysis you can simplify some lines:
for (int i=1; i< (n/2); i++)
This can be simplified to:
for (int i=1; i<n; i++)
Therefore it's O(n) as already identified by you.
for(double j =0; j <100; j+= (100.0/n)
This can be simplified as:
for(double j =0; j <1; j+= (1.0/n) (divided by 100)
for(double j =0; j <n; j+= 1.0) (multiplied by n)
And again, a simple O(n) loop.
doSomething (n/2);
That is by definition a O(n) statement.
So in total you have O(n)*O(n)*O(n), which is O(n^3).
Question 3
int f(n) {
if (n<1) return;
g(n-1);
}
void g(n) {
f(n/2)
doOne(); // doOne has time complexity O(1)
}
Not sure how you got the O(n*log(n)) here because not every n value is checked. In fact you start at n and the steps are n/2, n/4, n/8, n/16, n/32, n/64, n/128, ... and at some point you reached the terminate condition if (n<1). This is a simple O(log(n)) loop.

Calulating time complexity of loops with in loops example

hi I was working with analysis of iterative solution, here is one problem that I am not able to calculate the worst-case running time
void function(int n)
{
int count = 0;
for (int i=0; i<n; i++)
{
for (int j=i; j< i*i; j++)
{
if (j%i == 0)
{
for (int k=0; k<j; k++)
printf("*");
}
}
}
}
Here is the link to above problem, https://www.geeksforgeeks.org/analysis-algorithms-set-5-practice-problems/
Please see problem no 7.
What is the time complexity of above function? In the problem, they say it is O(n^5) but I have some doubts about it can somebody provide me some mathematical proof
First of all, your program will crash because if(j % i == 0), both i and j are 0
changing your code a bit
void function(int n)
{
int count = 0;
for (int i=1; i<n; i++) // runs O(n) times
for (int j=i; j< i*i; j++)
if (j%i == 0) // runs O(i) times
{
for (int k=0; k<j; k++) // runs j times, whenever j is factor of i
//that is when j = i, j = 2i ... j = i* i
printf("*");
}
}
take an example when i = 5
This implies total complexity is
for (int j=5; j< 25; j++)
if (j%i == 0) // runs O(i) times
{
// runs j times when j = 5, 10, 15, 20
for (int k=0; k<j; k++) {
printf("*"); // runs j times when j = 5(1 + 2 + 3+ 4)
// runs j times which is i*i*(i*(i-1)/2) times
// runs i^4 times
}
}
this implies total complexity is O(n^4)
I think it might be O(N^5). The first for loop has a range up to n, the second has n^2(considering the highest coefficient term), and the last one is same with the second one. Thus the multiplication of these are n^5.

Resources