in Algorithms
1,481 views
4 votes
4 votes
 for(int i=0; i < n; i++)
 {
    for(int j=0; j < (2*i); j+=(i/2))
    {
	cout<<"Hello Geeks";
    }
 }

is it o(nlogn)??

in Algorithms
1.5k views

4 Comments

@gauravkc

inner loop j not always executing 4 times only for n=2,4,6,8....means all even numbers after 0 execute for 4 times...i.e it will execute in total...4* (n/2)  

now for odd terms i.e 5,7,9,11.... it execute for 5 times for 3 only it executes 6 times...so overall time is complexity is O(n) only..

(as i integer it will take only int value in case when i is odd when you divide you dont get int values..so there is difference in number of times loop is executing for different i values..)

correct me if i am.wrong.
0
0
For odd values it is running 5 times, correct. So, overall must be O(4.5n) since half of the times it will run 4 times and half of the time it'll run for 5 times.
0
0
yeah overall it would be O(n) only..
0
0

Please log in or register to answer this question.