Teaser - 4

19 replies [Last post]
bOtskOOl
Offline
Last seen: 10 years 3 weeks ago
Joined: 2009-02-21
bOt Points: 3500

 

 

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.

 

 

 

 

 

heloitsadi
Offline
Last seen: 10 years 34 weeks ago
Joined: 2009-03-15
bOt Points: 1321
What does this mean... at

What does this mean... at O(1)

aashiqcoolguy21
Offline
Last seen: 9 years 50 weeks ago
Joined: 2009-05-31
bOt Points: 3354
third question is very

third question is very interesting yar..

 

 

bOtskOOl
Offline
Last seen: 10 years 3 weeks ago
Joined: 2009-02-21
bOt Points: 3500
Big O Notation

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.

heloitsadi
Offline
Last seen: 10 years 34 weeks ago
Joined: 2009-03-15
bOt Points: 1321
Ok thanks a lot...

Ok thanks a lot... 

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
ravinder singh_teaser 3

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]);

 

}

 

 

aashiqcoolguy21
Offline
Last seen: 9 years 50 weeks ago
Joined: 2009-05-31
bOt Points: 3354
Ye ah ravinder.. thnx for the

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

 

 

heloitsadi
Offline
Last seen: 10 years 34 weeks ago
Joined: 2009-03-15
bOt Points: 1321
ravinder can you explain your

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

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
ravinder singh_teaser 4

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 

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
yaa sure

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.

nitesh
Offline
Last seen: 9 years 36 weeks ago
Joined: 2009-03-26
bOt Points: 370
Incorrect

@ravinder

good attempt but it was incorrect .

It has been edited to remove the other entries

heloitsadi
Offline
Last seen: 10 years 34 weeks ago
Joined: 2009-03-15
bOt Points: 1321
Ya

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..

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
thanx for ur comment. but can

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.

nitesh
Offline
Last seen: 9 years 36 weeks ago
Joined: 2009-03-26
bOt Points: 370
@ravinder

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

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
This will be f9 but for more

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]);

 

 

 

heloitsadi
Offline
Last seen: 10 years 34 weeks ago
Joined: 2009-03-15
bOt Points: 1321
1.)commport |= (1 <<

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 5th 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 5th bit (or element you can say).

 

Can you please explain how this is happening...

Please reply soon.

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
ravinder singh_teaser 4

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();
}

shubham
Offline
Last seen: 7 years 37 weeks ago
Joined: 2009-05-28
bOt Points: 134
@ravinder your answer to

@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.

 

Sample Output

 

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

 

 

ravinder singh
Offline
Last seen: 10 years 42 weeks ago
Joined: 2009-06-11
bOt Points: 124
right answer for problem 1

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");

 }

shubham
Offline
Last seen: 7 years 37 weeks ago
Joined: 2009-05-28
bOt Points: 134
Bingo!!

@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.