retagged by
418 views
0 votes
0 votes

i think answer must be O(1) , as whether n is power of 2 or not,result of if condition is always true

retagged by

1 Answer

Best answer
0 votes
0 votes

I don,t how will you say if condition is always true. I explain through small example.

#include <stdio.h>
#include<stdbool.h>int main()
{
    printf("Hello World");
    int n =7;
    bool s = !(n & (n-1));
    printf("%s\n",s?"true":"False");

    return 0;
}

 output of !(n & (n-1)) is false if n is odd like 7,9,1 e.t.c  

false && (true/ false) is always false. so if n is odd number then if condition is not satisfied and then enter into while condition that take O(logn) time complexity.

If anything you feel wrong plz..   comment !

selected by

Related questions

0 votes
0 votes
0 answers
1
Neelu Lalchandani asked Nov 2, 2022
337 views
Time Complexity in C will be O(n) right? and big omega (n) is also big omega (n^2), then why is c incorrect?
0 votes
0 votes
1 answer
2
3 votes
3 votes
1 answer
3
Daniyal89 asked Oct 8, 2018
1,032 views
ans given is option A
0 votes
0 votes
1 answer
4