Re: [algogeeks] Re: BIT MANIPULATION

2011-07-11 Thread raj singh
@yogesh your code is simple one and excellent to understand; -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...

[algogeeks] Re: BIT MANIPULATION

2011-07-10 Thread Dave
@DK. Yes. But you must admit that it is a bit-twiddler's delight! Dave On Jul 10, 3:58 pm, DK wrote: > @Dave: Thanks for the link. > Just a point of discussion - this kind of code would probably never pass > code-review (or would be heavily documented with references and warnings > that say HAND

[algogeeks] Re: BIT MANIPULATION

2011-07-10 Thread DK
@Dave: Thanks for the link. Just a point of discussion - this kind of code would probably never pass code-review (or would be heavily documented with references and warnings that say HANDS OFF ;) ) -- DK http://twitter.com/divyekapoor http://www.divye.in -- You received this message because

[algogeeks] Re: BIT MANIPULATION

2011-07-09 Thread Dave
@Piyush: The referenced web page includes a powerpoint presentation that explains it. Did you read the presentation before asking? If not, why not? Dave On Jul 9, 5:04 pm, Piyush Sinha wrote: > @Dave..canu u explain ur algo to approach this formula?? > > On 7/10/11, Dave wrote: > > > > > > > @P

Re: [algogeeks] Re: BIT MANIPULATION

2011-07-09 Thread Piyush Sinha
@Dave..canu u explain ur algo to approach this formula?? On 7/10/11, Dave wrote: > @Piyush: See http://groups.google.com/group/algogeeks/msg/2b64c4f96fa3598e > for a one-line algorithm. > > Dave > > On Jul 9, 4:23 am, Piyush Sinha wrote: >> I found a good question to try for bit manipulation

[algogeeks] Re: BIT MANIPULATION

2011-07-09 Thread Dave
@Piyush: See http://groups.google.com/group/algogeeks/msg/2b64c4f96fa3598e for a one-line algorithm. Dave On Jul 9, 4:23 am, Piyush Sinha wrote: > I found a good question to try for bit manipulation.Try it... :) > > Given an integer x, find out the smallest integer which has same > number of

Re: [algogeeks] Re: Bit Manipulation

2011-01-18 Thread Terence
No. It can be applied to any positive number N. The solution above splits the numbers by the least significant bit, choose the group with missing number, and then drop the last bit and continue. In this way the set [0,N] is partitioned (almost) evenly into 2 groups: {2k | k=0,1,...,N/2} and

[algogeeks] Re: Bit Manipulation

2011-01-17 Thread anurag.singh
Yes, Following should do for next smallest: 1. Find rightmost 01 pair 2. swap these two bits (make it 10) e.g. N=3(011), Next smallest: 5(101) N=10(1010), Next smallest: 12(1100) N=14(01110), Next Smallest: 22(10110) On Jan 18, 12:48 am, juver++ wrote: > @abobe > my solution is wrong when number

[algogeeks] Re: Bit Manipulation

2011-01-17 Thread anurag.singh
Yes, I guess following correction in step 1 for Next Smallest should fix it: 1. Find the leftmost 1 [ Not rightmost]. On Jan 18, 12:48 am, juver++ wrote: > @abobe > my solution is wrong when number is even (but it can be avoided with some > corrections into an implementation), > btw, you have a m

[algogeeks] Re: Bit Manipulation

2011-01-17 Thread anurag.singh
Yes, I guess following correction in step 1 for Next Smallest should fix it: 1. Find the leftmost 1 [ Not rightmost]. On Jan 18, 12:48 am, juver++ wrote: > @abobe > my solution is wrong when number is even (but it can be avoided with some > corrections into an implementation), > btw, you have a m

[algogeeks] Re: Bit Manipulation

2011-01-17 Thread juver++
@abobe my solution is wrong when number is even (but it can be avoided with some corrections into an implementation), btw, you have a mistake: N=3(011), Next smallest: 6(110) ,* Should be 101 (5)!* In other cases my version is correct. -- You received this message because you are subscribed t

[algogeeks] Re: Bit Manipulation

2011-01-17 Thread anurag.singh
Q1 is very well answered by Sunny but looks like Q2 is still open. IMHO, juver++ soln on Q2 doesn't look correct to me (OR I didn't understand the problem). if given no is N=2 (010), then next smallest no N+1 = 3 (011) is not the right answer as it has TWO 1's. I guess, for N=2 (010), next smallest

Re: [algogeeks] Re: Bit Manipulation

2011-01-17 Thread juver++
@Sunny Good! -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this

Re: [algogeeks] Re: Bit Manipulation

2011-01-17 Thread sunny agrawal
Q1: initially compute xor of all the values from 0 to n in a variable Temp so temp = 0^1^2^n let result is used to store the missing number for each ith bit of missing number where i = 0-31 we can find it as following ith bit of result = (xor of all ith bits of values of array) xored with (

[algogeeks] Re: Bit Manipulation

2011-01-17 Thread awesomeandroid
@juver i am really sorry ,i forget to mention.ya this soln will work only if n is even power of 2. Regards Priyaranjan http://code-forum.blogspot.com On Jan 17, 12:43 pm, juver++ wrote: > @awesomeandroid > Your solution for Q1 is wrong. It can be applied only for such numbers N = > 2^k, so numb

Re: [algogeeks] Re: Bit Manipulation

2011-01-17 Thread juver++
@above, no :) it is solvable in linear time. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.co

Re: [algogeeks] Re: Bit Manipulation

2011-01-17 Thread Sharath Channahalli
I think Q1 is NP hard problem since the number of bits grows exponentially as the array size increases. On Mon, Jan 17, 2011 at 1:13 PM, juver++ wrote: > @awesomeandroid > Your solution for Q1 is wrong. It can be applied only for such numbers N = > 2^k, so number should power of 2. > > -- > You

[algogeeks] Re: Bit Manipulation

2011-01-16 Thread juver++
@awesomeandroid Your solution for Q1 is wrong. It can be applied only for such numbers N = 2^k, so number should power of 2. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To

[algogeeks] Re: Bit Manipulation

2011-01-16 Thread awesomeandroid
for Q1. you can do it in o(n) time Call your missing number M. You can split your array into two parts depending on whether the least significant bit of A[i] is a 1 or a 0. The smaller of the two parts (call it P_1) is at most (n-1)/2 elements in size, and it tells you whether M's least significan

[algogeeks] Re: Bit Manipulation

2011-01-16 Thread awesomeandroid
Q2.next higher number having equal number of 1's http://code-forum.blogspot.com/2011/01/next-higher-number-having-equal-number.html Regards Priyaranjan On Jan 16, 9:09 pm, Decipher wrote: > Q1)An array A[1   n] contains all the integers from 0 to n except for one > number which is missing . In t

[algogeeks] Re: Bit Manipulation

2011-01-16 Thread juver++
Q1: one of the solution implies DP: count[i][t] - how many bits with value t (t is either 0 or 1) are in all numbers from 0 to n at position i. Then for each position count actual number of bits 0 and 1, and determine missing bit. Set up missing bit in the final result. -- You received this me

[algogeeks] Re: Bit Manipulation

2011-01-16 Thread juver++
Q2: next smallest: if N is even (so, its least significant bit is 0) simply add one bit, Next_Smallest = N + 1 else { //locate rightmost 0 in the binary representation of N at index X, //at index X-1 there is 1, so you need to swap it with 0 at X. K = N+1, after this rightmost consecu