# Teaser - 4

1. Write a code to determine if a number is a power of 2 at O(1).

2. Given two arrays A and B. Array 'A' contains all the elements of 'B' but one more element extra.....

Find out the extra element......

**Restrictions**: Dont use any relational ops ( > or > or == etc....),

array elements are not in order ...,

3. Write a program that runs indefinitely .

pretty easy.........

The condition is that you cannot use any loops or declare any functions

**30 bOt points to be won**

NOTE::

- Points will be awarded to the first post with ALL 3 correct entries.

So dont post single answers and let others take credit for your effort.

- Please make sure that your code compiles and runs without any error.

What does this mean..

. at O(1).third question is very interesting yar..

O(n) called the Big O Notation is used as a measure of the time and space complexity of algorithms.

As far as time complexity is concerned the running time of any program is usually determined by the number of comparisons made. Thus O(1) means with a time complexity of 1, i.e. in a single comparsion.

Ok thanks a lot...

As I joined this commumnity little late, i saw the teasers after they are closed. But I have the best solution to the Teasure 3,i.e., even / odd problem... Plz have a look at it...My solution even prints Even or Odd.....

main()

{

char temp[] = "EvenOdd";

int i;

scanf("%d", &n);

printf("%.4s", &temp[(n & 1) << 2]);

}

Ye ah ravinder.. thnx for the abve .. solution it is really cool

ravinder can you explain your code.. i have bit of problems understanding shift operators..

1. Write a code to determine if a number is a power of 2 at O(1).

main()

{

unsigned int num;

scanf("%d",&num);

if( num & ( (~num)+1) == num)

printf("number is power of 2");

else

printf("number is not the power of 2");

}

EDITED

if the LSB(least significant bit) of a number is 1 then d number is odd else even.to know abt d LSB is 1 or 0. We use & operator;

here (x&a)<<2 return d index of temp array i.e either 0 or 4.

hope u got it.

@ravinder

good attempt but it was incorrect .

It has been edited to remove the other entries

Ya I got it.. that's really cool.. If I right...

char temp[] = "OddEven";

then this will give 'O' when Even..

& 'v' when Odd...

Am i right..

thanx for ur comment.

but can u just tell me which prog. was incorrect.so that i can explain.bcoz at my side it was working f9.

the one displayed above was incorrect . Just give it a more closer insight

This will be f9 but for more appropraite answer we are using it.This will print whole as even or add which can be easily understandable

char temp[] = "EvenOdd";

scanf("%d", &n);

printf("%.4s", &temp[(n & 1) << 2]);

1.)

commport |= (1 << en);This command sets value of en as 1, where commport is an 8-bit register (or array you can say) and en is the 5

^{th}bit (or element you can say).2.)

commport &= ~(1 << en);This command sets value of en as 0, where commport is an 8-bit register (or array you can say) and en is the 5

^{th}bit (or element you can say).Can you please explain how this is happening...

Please reply soon.

1)

main()

{

unsigned int num;

scanf("%d",&num);

if( num & ( (~num)+1) == num)

printf("number is power of 2");

else

printf("number is not the power of 2");

}

2)

main()

{

char array1[] ={'a','m','i','t'};

char array2[] ={'c','m','i','a','t'};

clrscr();

int i=0,size1 = sizeof(array1),j=0;

while(size1^i)

{

if(!(array1[i] ^array2[j]))

{

j++;

i=0;

}

else

i++;

}

printf("%c",array2[j]);

}

3)

main()

{

main();

}

@ravinder

your answer to Question 2 and 3 are correct but answer to Quest 1 still has got some problem.

I am pasting sample output which is produced by your program for your reference.

This Teaser is closed for everyone except ravinder because he has almost done correctly.

No points will be given to anyone else.

Thanks.

Happy Coding

yaa u r right,dere is a little mistake.now i post d correct answer.thanks for dis

main()

{

unsigned int num;

scanf("%d",&num);

if(( num & ( (~num)+1) ) == num)

printf("number is power of 2");

else

printf("number is not the power of 2");

}

@ravinder

now u r rite, u got the rite answer.

All three correct now.

Your points will be updated in your tally.

This teaser is fully closed now.