Thursday, March 24, 2011

masterpiece of bitwise operation...



What does this piece of code do?

It finds one single unique number in an array where all other numbers appear three times. This is essentially mod3.

Explanation (Thanks to Rama B@careercup)

"The code works in similar line with the question of "finding the element which appears once in an array - containing other elements each appearing twice". Solution is to XOR all the elements and you get the answer.

Basically, it makes use of the fact that x^x = 0. So all paired elements get XOR'd and vanish leaving the lonely element.
Since XOR operation is associative, commutative.. it does not matter in what fashion elements appear in array, we still get the answer.

Now, in the current question - if we apply the above idea, it will not work because - we got to have every unique element appearing even number of times. So instead of getting the answer, we will end up getting XOR of all unique elements which is not what we want.

To rectify this mistake, the code makes use of 2 variables.
ones - At any point of time, this variable holds XOR of all the elements which have
appeared "only" once.
twos - At any point of time, this variable holds XOR of all the elements which have
appeared "only" twice.

So if at any point time,
1. A new number appears - It gets XOR'd to the variable "ones".
2. A number gets repeated(appears twice) - It is removed from "ones" and XOR'd to the
variable "twice".
3. A number appears for the third time - It gets removed from both "ones" and "twice".

The final answer we want is the value present in "ones" - coz, it holds the unique element.

So if we explain how steps 1 to 3 happens in the code, we are done.
Before explaining above 3 steps, lets look at last three lines of the code,

not_threes = ~(ones & twos)
ones & = not_threes
twos & = not_threes

All it does is, common 1's between "ones" and "twos" are converted to zero.

For simplicity, in all the below explanations - consider we have got only 4 elements in the array (one unique element and 3 repeated elements - in any order).

Explanation for step 1
------------------------
Lets say a new element(x) appears.
CURRENT SITUATION - Both variables - "ones" and "twos" has not recorded "x".

Observe the statement "twos| = ones & x".
Since bit representation of "x" is not present in "ones", AND condition yields nothing. So "twos" does not get bit representation of "x".
But, in next step "ones ^= x" - "ones" ends up adding bits of "x". Thus new element gets recorded in "ones" but not in "twos".

The last 3 lines of code as explained already, converts common 1's b/w "ones" and "twos" to zeros.
Since as of now, only "ones" has "x" and not "twos" - last 3 lines does nothing.

Explanation for step 2.
------------------------
Lets say an element(x) appears twice.
CURRENT SITUATION - "ones" has recorded "x" but not "twos".

Now due to the statement, "twos| = ones & x" - "twos" ends up getting bits of x.
But due to the statement, "ones ^ = x" - "ones" removes "x" from its binary representation.

Again, last 3 lines of code does nothing.
So ultimately, "twos" ends up getting bits of "x" and "ones" ends up losing bits of "x".

Explanation for step 3.
-------------------------
Lets say an element(x) appears for the third time.
CURRENT SITUATION - "ones" does not have bit representation of "x" but "twos" has.

Though "ones & x" does not yield nothing .. "twos" by itself has bit representation of "x". So after this statement, "two" has bit representation of "x".
Due to "ones^=x", after this step, "one" also ends up getting bit representation of "x".

Now last 3 lines of code removes common 1's of "ones" and "twos" - which is the bit representation of "x".
Thus both "ones" and "twos" ends up losing bit representation of "x".

1st example
------------
2, 2, 2, 4

After first iteration,
ones = 2, twos = 0
After second iteration,
ones = 0, twos = 2
After third iteration,
ones = 0, twos = 0
After fourth iteration,
ones = 4, twos = 0

2nd example
------------
4, 2, 2, 2

After first iteration,
ones = 4, twos = 0
After second iteration,
ones = 6, twos = 0
After third iteration,
ones = 4, twos = 2
After fourth iteration,
ones = 4, twos = 0

Explanation becomes much more complicated when there are more elements in the array in mixed up fashion. But again due to associativity of XOR operation - We actually end up getting answer."


Wednesday, March 23, 2011

Selecting the largest/smallest N numbers out of M numbers

Given sufficient amount of memory to hold all M.

  1. Your gut feeling that this can be a heap sort. O(n*log(n)).
    Don't be shy, say it out load, it is indeed correct and efficient. But also, do not settle.

  2. Your can also do this by using quick sort, by adjusting the next pivot accordingly after partition. Still O(n*log(n)), but in real world the gain here is actually quite significant as you can ignore at least one of the two partitions each time.

  3. In #2, you may have noticed the fact that you are not required to sort the N number. But taking that one step further. You can apply Selection Sort, which is O(n), to find the Nth number during one iteration, and you should have guessed what steps to follow when you have the Nth number at hand.
Even if you want to have the N number sorted, #3 is still the optimal solution as you can take the N number and sort them in O(Nlog(N)) time, which is smaller than O(Mlog(M)) as N.

Update: It seems that I was wrong about #2. #2 is actually linear. See here. Randomized-Select is the name, and it is introduced in Chapter 9 of "Introduction of Algorithms" 2nd Edition.

Read the book, as always...

Monday, March 14, 2011

recursively removing files with wildcard in linux

If you are a Linux rookie as me, you might expect this to work, as well as I did:
rm -r *.class

Bozinga! this only deletes the *.class under wherever you are, because wildcards are expanded before executing the commands.

here is a non-trivial command that does the seemingly trivial magic you wish:
find /path_to_search -type f -name "*the_patern*" -exec rm {} \


Thursday, March 10, 2011

Installing Ubuntu 10.10 on my Thinkpad T60

Just for future reference. Should write a bash script to automate this.
  • Ubuntu Screen flickering/flashing problem with ATI x1300/x1400
    cat /etc/modprobe.d/radeon-kms.conf 
    options radeon modeset=1
    reboot
  • Install Java
    Turn on "Partner" in repo list
    sudo apt-get update  
    sudo apt-get install sun-java6-jdk
  • Install Aptitude
  • Install chrome, dropbox, eclipse
  • Sync VI configuration, Sync Terminal configuration, Sync .bash_aliases
  • Layout Dev, Workspaces, git clone, etc