user2393690
user2393690

Reputation: 33

What is the big-O complexity of this code

Whats the big-O notation of this code?

for( int i=1; i<2n; i++)
   x=x+1;

My answer = O(2*n) Is this correct?

Upvotes: 2

Views: 85

Answers (3)

MChaker
MChaker

Reputation: 2649

Consider this an A algorithm

for( int i=1; i<2*n; i++)
 x=x+1;

Algorithm A’s run-time: T(n) = 2n-1

  1. Eliminate lower-order terms: 2n-1 -> 2n
  2. Drop all constant coefficients: 2n -> n

So the algorithm A’s time complexity is O(n).

Upvotes: 3

NendoTaka
NendoTaka

Reputation: 1224

The big-o run time of this is O(2n) like you guessed but that is usually just simplified to O(n).

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180434

It is O(n). Big O is meant to describe the complexity of the application and in this case it is linear so it is O(n).

Upvotes: 0

Related Questions