1 votes 1 votes Please help to find the time complexity of this loop. void main() { int n; while(n>=1) { n=n-2; } } Algorithms time-complexity algorithms + – Devshree Dubey asked Mar 19, 2019 Devshree Dubey 664 views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply abhishekmehta4u commented Mar 19, 2019 reply Follow Share n/2---> O(n) 1 votes 1 votes Anuranjan commented Jun 21, 2019 reply Follow Share Here the value of n is decreasing by 2.. so the while loop will run for n/2 times..hence it will be O(n) 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes Since each time the n is decreasing by 2 so overall iterations will become half and time complexity=n/2 Winner answered Mar 19, 2019 Winner comment Share Follow See all 3 Comments See all 3 3 Comments reply Devshree Dubey commented Mar 19, 2019 reply Follow Share Yeah I know that it is getting reduced by 2,but why does it becomes half? 0 votes 0 votes Winner commented Mar 19, 2019 reply Follow Share Let us suppose a scenario where n is decreasing by 1 in each loop, so in order to reach 1 it will take n iterations , n=6 to 1 take 6 iterations,now in question here n is decreasing by 2 like if n=6 it will become 4 then 2 and then 0 so the iterations are reduced by half it will take 3 iterations so n/2 time =O(n) 2 votes 2 votes Devshree Dubey commented Mar 19, 2019 reply Follow Share @Winner,Thank you so much brother. This is where I wanted to get. :).So the instructions will just be half. A tonnes Thanks. 1 votes 1 votes Please log in or register to add a comment.