How to find Complexity? - for-loop

How to find the time complexity {Big-Oh} of the following function?
function(int n) {
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(" * ");
}
}
}
}
}
Answer for this is O(n^5) but I don't know how to find that.

I think that complexity is O(n^5).
function(int n) {
for (int i = 0; i < n; i++){ // N, max value of i is n
for (int j = i; j < i*i; j++){ // N^2, max value of j is n*n
if (j%i == 0){
for(int k = 0; k < j; k++){ // N^2, max value of k is n*n
printf(" * ");
}
}
}
}
}
The first loop is N. Second loop the max value of j is i*i => n*n so it is n^2. Third loop, the max value of k is j, which is n*n so n^2. So n * n^2 * n^2 is n^5

Related

Time complexity of this algorithm? How to analysis?

Time complexity of this algorithm? How to analysis?
int fun(int n)
{
int i = 0, j = 0, m = 0;
for (i = n; i > 0; i /= 2)
{
for (j = 0; j < i; j++)
{
m += 1;
}
}
return m;
}
Your running time is Sum_i (n/2^{i}), for i in {0 to log(n)}. n is the leading term, thus O(n). The sum will not exceed 2n.

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;
}

Calculating time complexity with big O

I have an assignment I am not sure with; I have to calculate the time complexity of the following code:
int a[][] = new int[m][n]; //O(1)
int w = 0; //O(1)
for (int i = 0; i < m; i++) //O(n)
for (int j = 0; j <n; j++) //O(n)
if (a[i] [j] % 2 == 0) //O(logn)
w++; //O(1)
So from my O estimations I add them up:
O(1) + O(1) + O(n) * ( O(n) * ( O(logn) + O(1) / 2 ) )
O(1) + O(1) + O(n) * ( O(nlogn) + O(n) / 2 )
O(1) + O(1) + (O(n2logn) + O(n2) / 2)
=O(n2logn)
I'm not sure if my train of thought is correct, could somebody help?
for (int i = 0; i < m; i++) //O(m)
for (int j = 0; j <n; j++) //O(n)
if (a[i] [j] % 2 == 0) //O(1)
w++; //O(1)
So the total complexity in terms of big-o is:
O(m)*(O(n) + O(1) + O(1)) = O(m)*O(n) = O(m*n).
for (int i = 0; i < m; i++) //O(m)
{
for (int j = 0; j <n; j++) //O(n)
{
// your code
}
}
So the i loop will go on m times, and for the j loop would run n times.
So in total the code will go on m*n times which would be its time complexity: O(m.n)
The final complexity is O(n^2)
Your logic is close except...
int a[][] = new int[m][n]; //O(1)
int w = 0; //O(1)
for (int i = 0; i < m; i++) //O(n)
for (int j = 0; j <n; j++) //O(n)
if (a[i] [j] % 2 == 0) //O(1)
w++; //O(1)
Your if statement embedded in your second for loop is simply referencing an element in an array and doing a basic comparison. This is of time complexity O(1). Also, typically you would not consider initializing variables in a time complexity problem.

Algorithmic big o order of growth code

I'm doing an online course and i'm stuck on this question. I know there are similar questions but they don't help me.
What is the order of growth of the worst case running time of the
following code fragment as a function of N?
int sum = 0;
for (int i = 0; i*i*i < N; i++)
for (int j = 0; j*j*j < N; j++)
for (int k = 0; k*k*k < N; k++)
sum++;
I thought that the order would be n^3 but I don't think this is correct because the loops only go through a third of n each time. So would that make it nlogn?
Also
int sum = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
for (int k = 1; k <= N; k = k*2)
for (int h = 1; h <= k; h++)
sum++;
I think this one would be n^4 because you have n * n * 0.5n * 0.5n
The loops in fact only go up to the cube root of N. (i^3 < n, etc.)
The 3 nested loops of this length, give O(cube root of N, cubed). This O(N)
Of note, if you were correct and they each went to one third of N, then cubing this still gives O(N^3/9), 1/9 is constant, so this is O(n^3)
If you examine the value of sum for various values of N, then it becomes pretty clear what the time complexity of the algorithm is:
#include <iostream>
int main()
{
for( int N=1 ; N<=100 ; ++N ) {
int sum = 0;
for (int i = 0; i*i*i < N; i++)
for (int j = 0; j*j*j < N; j++)
for (int k = 0; k*k*k < N; k++)
sum++;
std::cout << "For N=" << N << ", sum=" << sum << '\n';
}
return 0;
}
You can then draw your own conclusions with greater insight.

How do I find the time complexity of these 3 nested loops?

The task is to analyze the following algorithm and calculate its time complexity.
I solved it as taking nested loops are 3 so O(n^3).
How do I solve this problem?
MSS (A[], N) //Where N is size of array A[]
{
int temp = 0, MS = 0;
For (int i = 0; i < N; i++)
{
for(int j = i; j < N; j++)
{
temp = 0;
for(int k = i; k <= j; k++)
temp = temp + A[k];
if(temp > MS)
MS = temp;
}
}
return(MS);
}
Well, you can proceed formally as such:

Resources