Big Big O (II)

1
2
3
4
5
6
7
8
9
for (int index=0; index<100; i++){
    cout<<Arr[index]<<" ";
}
cout<<endl;
int sum=0
for (int index=0; index<100; i++){
    sum+=Arr[index];
}
cout<<sum<<endl;

The C++ code given above runs in __________ \text{\_\_\_\_\_\_\_\_\_\_} .


This problem is a part of the set: Efficient Algorithms .
O ( n 2 ) O(n^2) O ( 0 ) O(0) O ( n ) O(n) O ( 1 ) O(1)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Zeeshan Ali
Feb 11, 2016

The first for-loop runs 101-times which implies that it is O ( 101 ) O(101) which is equivalent to O ( 1 ) O(1) . Same is the case with the second for-loop and: O ( 1 ) + O ( 1 ) = O ( 1 ) O(1)+O(1)=O(1) Therefore, we say that the code runs in O ( 1 ) O(1)

Note: Unless a loop runs n n times for any non-negative integer n n , it remains O ( 1 ) O(1)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...