What is the Big O notation of a loop in loop - algorithm

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

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.

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.

How to calculate time complexity for below function?

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

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:

What will be the complexity of for loop if nothing is happening in the body of loop

Code:
int c = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
c = i * j;
}
}
Time Complexity: O(n2)
Now what will be the complexity of following code:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
//c = i * j;
// nothing is happening inside the loop
}
}
whether complexity will be same as above( O(n2) ) or something else??
Theoretically - yes because there is still the issue of increasing the i and j which still needs to happen, and comparing them to the end value in each iteration.
However - compilers might optimize it to be done in constant time, and just set the post values of i and j.
For both complexity is O(N^2).

Resources