Friday, June 14, 2013

the sorted Array and the scanner problem


I came across this puzzle during my Msc(Engg) interview in IISc Bangalore. The puzzle was simple and stated as -

"You are given a sorted array of length n and a pointer that points to the first element in the array. This is a special pointer that can be reversed in direction only once. That means if it was moving to right and then it decides to move towards left then it keeps on moving to left. It cannot change its direction again."

The problem is to come up with an efficient algorithm to search a number in the array.

While it took me fews seconds to put all the conditions and situations together, my first response was to do a linear search. And that was accepted quite generously. Of course, the next question as anticipated was "Can you do better?"

This algorithm had a running time of n. So I decided to divide the array into two parts. I suggested directly compare the middle element with the search element and depending upon the outcome, we move into either of the half. This reduced the running time to n/2. That too was accepted cordially and followed by the same question!

Instead of targeting the middle element, we could target, say n/3th element. So running time is down to n/3 from my first response of n. Next question was a bouncer.

"So what's the appropriate step size that gives the best efficiency?"

He meant, we could go endlessly by choosing n/5 th, n/10 th, n/20 th and so on. Which one of them would be the best?

Suddenly I recalled a similar problem that I had read long back on 20bits.com, which tries to find similar solution. But I couldn’t recollect the way he had solved, probably because four IIsc professors were continuously gazing me. I tried recollecting and gave up. But I gave them a brief idea how could we find the solution. They looked satisfied with my broken answer and asked me to ponder upon it after the interview.

Soon, I was heading back home in a BMTC bus. My mind was completely occupied with calculations and trying to find the solution for that problem. But I did it! I found it in BMTC!!

It was not that difficult as it sounded in the interview room. We wanted to find the step size that would lead best efficiency. So If I choose n/3 step size, then once finding the appropriate partition I had to do a linear search for remaining 2 elements in the partition. The explanation being, since each partition had 3 elements and one of them gets compared at each step size, I had to check the remaining 2 using linear search. What if I take step size n/10? I needed to check another 9 elements using linear search in the partition.

So, mathematically put, the total number of comparisons required –

n/s + (s-1)

where

n -> array length and s -> step size.

This equation preciously defines the number of comparisons required for a generic step size s. The first part of the equation: n/s, indicates the number of partitions that are formed due to step size s and these were the number of comparisons required on the step size in worst case, as the search element could be in the last partition. The other part: (s-1), indicates the number of linear comparisons required once we found our partition. Since each partition had s elements and one of them was already compared during partition searching, we required only s-1 more comparisons.

Now we have a mathematical representation of the problem we are dealing with and we just needed to find the step size that resulted into minimum comparisons. Hence we simply needed to find the value of s where the above equation had a minima. And that’s typical differentiation stuff. We differentiate the above equation wrt s and equate it to zero to get the required value of s.

Thus on differentiating and equating the above equation to zero,

-n/s2 +1 =0

=> n = s2

Or s = √n

 i.e. s = sqrt(n)

And that’s it!

continued...

Rebooting ...


I'm back here. It's been really long long time, I penned here. But the news is, yes! I have decided to write again. System's rebooting! The look has been updated and the site has got new address http://my1bit.blogspot.in/  from previous one omsaiindia.blogspot.com. Plus, I have merged my another blog: bitsnthebytes.blogspot.com with this! Get ready, the fun begins now ...

Saturday, July 18, 2009

PSP


Ah..finally listening to PSP while blogging..Generally I blog about technical stuffs, this is my first attempt to jot down about something which I was dreaming of since past 6-7 months.


Yes I can finally say, Sony PSP has made me write this. I exceptionally love this gadget, it’s a complete modern world class widget that every teenager would love to play with. Aside from playing high end games, this device completely chills you out. Its a ultimate combo gizmo. It has varied game tittles all around world from EA to Activision..from First point shooting to Car Racing...from 10 Mb to 2 Gb...and the graphics are amazing...superior quality and makes you feel you are on high end PC.


And mind you..its a big time portable music player. Unique sound quality with marvellous visualizers and presets.
The GUI is stunning..presents itself as a alien gizmo.
4.3 inches wide screen gives you theatrical video experience. The quality is superb as it renders DVD print.. MP4 format.
Viewing photos on the go is an additional feature just to revive your nostalgic moments.


Finally how can I forget to mention Homebrew in this blog. Yes, you need to hack your PSP to enjoy Homebrew applications, including playing games directly from memory stick. This made me hack my brand new PSP that was 5 days old ...wow nice topic. The reason I hacked is not that I endorse piracy or so..its just for educational purpose.. just to improve my GK ;) Very soon I will be coming with some nice and simple instructions on how to hack your PSP and enjoy it to fullest.


So till then keep watching this space...Myth Signing off!!

P.S – The hack coming in next blog is a temporarily hack made in PSP’s RAM. So if you shut it off, you get back your original PSP. I strongly recommend not to try hacks those touch PSP’s flash areas.

Friday, June 26, 2009

Google Squared !!

It makes me keep an eye over Google lab. Every now and then they keep adding stuffs, hmm..rather intelligent stuff. i found a new one.. have a look at it - GOOGLE SQAUARED
I always try to be in new technology and features - especially when it comes to Google (they can be extremely innovative and think way out of the box when it comes to usability and features for web experience!). So check out Google Squared Labs!Google Squared is an experimental search tool that collects facts from the web and presents them in an organized collection, similar to a spreadsheet. If you search for [roller coasters], Google Squared builds a square with rows for each of several specific roller coasters and columns for corresponding facts, such as image, height and maximum speed.The great thing is that it combines all the information on the web on to one place, so you’re not clicking on multiple links (and then the back button) when looking for information.This technology is by no means perfect, enabling you to respond to the initial result and get a better answer. If there's another row or column you'd like to see, you can add it and Google Squared will automatically attempt to fetch and fill in the relevant facts for you. As you remove rows and columns you don't like, Google Squared will get a fresh idea of what you're interested in and suggest new rows and columns to add.
Just try giving gaming consoles, and you get gaming consoles all around the world properly arranged in a spreadsheet. If you feel some information is lacking, just click add more columns and fill in thats it. Just for eg, in my case there was no price displayed initially, i just added a column with name price and bingo it displayed the current prie of all devices..amazing..really a cool link to play with and fullfill your desirations.
If you click on any fact, you'll see the sources Google Squared gathered it from as well as a list of other possible values that you can investigate. So even if your square isn't perfect at the beginning, it's easy to work with Google Squared to get a better answer in no time. Once you've got a square you're happy with, you can save it and come back to it later.This type of technology has obvious applications for many types of targeted searches, including product search, health search, scientific searches, you name it. There are dozens of semantic search startups trying to impose structure on the Web to perform similar tricks. Another high-profile search startup which is launching is Wolfram Alpha, takes a slightly different approach in that it simply ingests massive amounts of information into its own databases where it can query it to its heart’s delight. Already there is a bit of a rivalry between Google and Wolfram because getting back structured results is a major new direction for search.


Saturday, January 24, 2009

If u r in love..this is for u..from Dr. Kumar Vishas

Love, pyar, Ishq,prem, mohabbat jane kitane naam

ye panktiyan 

isaki mehak me dube har ek diwane ke naam

 

Koi diwana kehta hai, koi pagal samajhta hai,

Magar dharti ki baichani ko bas badal samajhta hai.

Main tujhse door kaisa hoon, tu mujhse door kaisi hai,

Yeh tera dil samajhta hai ya mera dil samajhta hai.


Mohabbat ek ehsason ki pawan si kahani hai,

Kabhi kabira diwana tha, kabhi mira diwani hai.

Yahan sab log kahte hain meri aankhon mein ansoon hain,

Jo tu samjhe to moti hai, jo na samjhe to pani hai.


Samandar pir ka andar hai lekin ro nahin sakta,

Yeh ansoon pyar ka moti hai, isko kho nahin sakta.

Meri chahat ko dulhan tu bana lena, magar sun le,

Jo mera ho nahin paya, woh tera ho nahin sakta.


Bhramar koi kumudini par machal baitha to hungama,

humaare dil mein koi khwab pal baitha to hungama.

Abhi tak dhoob kar sunte the sab kissa mohabbat ka,

Main kisse ko haqiqat mein badal baitha to hungama


watch out the video at: 

http://www.youtube.com/watch?v=zaWw0Oi23OQ

Listen at :

 http://www.satyamyadav.com/audio/awesome.mp3