guy_without_a_name
guy_without_a_name

Reputation: 155

Two for loops, big O theory

I have this loop running inside a program:

for(int I =0;I < n;I++){
   for(int it = 0; it < m; it++){

       Access vector.at(it+1) & add number plus vector.at(it)
        }
  }

Both n & m are user input and what I want to do is run the inside loop the size of the vector (m) and store information. The outside loop is saying to repeat that process n times. So would my big O notation be O(m^n) since I'm repeating m however many times n is? Thanks.

Upvotes: 0

Views: 307

Answers (3)

JBL
JBL

Reputation: 12907

You're performing 2 operations in the inside loop, thus you are doing a total of 2 * n * m operations, which gives a O(n*m) complexity.

Upvotes: 1

chepner
chepner

Reputation: 530960

It is O(mn), assuming that the operation inside the inner loop is O(1).

Upvotes: 1

Forhad Ahmed
Forhad Ahmed

Reputation: 1771

It would actually be O(M x N)

O(M^N) is very very slow :)

Upvotes: 1

Related Questions