Problem: you are given a sequence of numbers from 1 to n-1 with one of the numbers repeating only once. (example: 1 2 3 3 4 5). how can you find the repeating number? what if i give you the constraint that you can’t use a dynamic amount of memory (i.e. the amount of memory you use can’t be related to n)?
what if there are two repeating numbers (and the same memory constraint?)

Solution

as a programmer, my first answer to this problem would be make a bit vector of size n, and every time you see the number, set its correspond index bit to 1. if the bit is already set, then that’s the repeater. since there were no constraints in the question, this is an ok answer. its good because it makes sense if you draw it for someone, whether they are a programmer, mathemetician, or just your grandpa. its not the most efficient answer though.

now, if i add the constraint that you can only use a fixed amount of memory (i.e. not determined by n) and it must run in O(n) time… how do we solve it. adding all the numbers up from 1 to n-1 would give us a distinct sum. subtracting the total sum of all the numbers from the sum of n to n-1 ( which is (n)(n-1)/2 ) would give us the secret extra number.

what if you can only use a fixed amount of memory, and TWO of the numbers are repeated? we know that the numbers have a distinct sum, and the difference would be equal to the sum of our unknowns c = a + b
where c is the sum and a and b are the unknowns – c is a constant
if we had another similar formula we could solve the two unknown equations. my first thought was that the numbers would have a distinct product – (n-1)!
if we divide the total product by the (n-1)! product, we would get another equation c2 = ab
we could then solve the two equations to get them into quadratic formula notation 0 = ax^2 + bx + c
and solve for the two values of x. this answer is correct but factorial grows really fast.

some sort of sum would be better. the sum of the squares from n-1 to 1 would work. that would yield a function of the form c2 = a^2 + b^2
which could also be solved by using the quadratic equation.

i think its fine to remind someone of the quadratic equation… (maybe only because i myself had to look it up to solve the problem) i mean really though, the last time i used it was probably in 10th grade. as long as they get the idea that given two unknowns and two equations you can solve for the unknowns – thats the point.

Mercedes-Benz F-CELL World Drive – 125 days, four continents and 14 countries: Starting on January 30, 2011 in Stuttgart (Germany), three Mercedes-Benz B-Class F-CELL with fuel cell drive will be driving around the globe with zero emissions. The objective is to demonstrate that electric vehicles equipped with fuel cell are technically mature and suitable for everyday use.

Mercedes-Benz F-CELL World Drive – 125 days, four continents and 14 countries: Starting on January 30, 2011 in Stuttgart (Germany), three Mercedes-Benz B-Class F-CELL with fuel cell drive will be driving around the globe with zero emissions. The objective is to demonstrate that electric vehicles equipped with fuel cell are technically mature and suitable for everyday use.

Electro chromatic mirrors / Auto dimming mirrors use a combination of opto electronic sensors and complex electronics (sensors, circuit boards, micro-controllers, etc.) that constantly monitor ambient light and the intensity of light shining directly on the mirror. As soon as sensors detect glare, the electro chromatic surface of the mirror becomes darker to protect driver’s eyes and their concentration.

The electro chromatic technology usually is applied to the inside rearview mirror / Side view mirror, where it basically saves you the trouble of flipping the mirror manually if blinded by the light which increases driving safety.

Working operation of Electro chromatic mirrors:

This auto dimming rearview mirror is installed on higher end vehicles. It consists of two lenses that sandwich an electro chromic (electronic color changing) gel. The inside sides of the lenses are coated with a transparent conductive layer coating and the deepest lens has a reflective coating.

This gel, when charged with electricity darkens, then clear once the glare is no longer detected. The mirror uses a forward sensor which measures the outside ambient light, and a rearward sensor to look for glare. When dark enough, it sends current to the electrostatic gel, darkening it a rate which is related to the level of ambient darkness and rearward glare. When the outside ambient light increases, the current decreases, until the gel is clear again at daylight light levels.

AIMES, Srinivas Integrated Campus,Mukka syndicates its weblog posts
and Comments using a technology called
RSS (Real Simple Syndication). You can use a service like Bloglines to get
notified when there are new posts to this weblog.