Walking Ants

Jaadu mailed me this problem today. I was able to solve it and I am happy. This is an "intelligent" problem. Solution so elegant that you will fall in love with it. :)

Assume 100 ants are moving in 1 dimensional plane, all move with the same speed. Some are moving towards the positive x axis and some towards negative. If a collision occurs between two ants both ants changes the direction. If you are given direction of motion of each ant, what is the number of collision that will occur? Give an algorithm. :)

Update (11/12/09): Solution by A Rustle (Prathmesh, CSE, IITB) in comments !!

Comments

  1. For an ant moving right, count the number of ants to its right moving left. Add this number for every right moving ant. That is the number of collisions.

    hint: on every collision, assume that the two ants don't change direction but simply cross each other.

    ReplyDelete
  2. @Rustle
    Great!! Correct!! Nice solution..

    ReplyDelete
  3. Its a nice problem. i was thinking of writing about...(trying to cp u)

    ReplyDelete

Post a Comment

Popular posts from this blog

Fraction Brainteaser

Buying Dimsums

Consecutive Heads