Zig-zag search

This is an interesting problem that I ran into. The problem definition goes like this.
You are given an array of integers. The array is considerably large (say 5 million elements). You are given two inputs: an index i in the array and an integer value v. Start searching the array for value v from the index i, and expand your search towards the two edges of the array. Return the index where the value v occurs closest to index i. If the value v occurs on both sides of index i at an equal distance, return the lower of the two indices. If the value v does not occur at all, return -1.
It was very interesting to solve this problem. Give it a shot, you might also like it.

Comments

Popular posts from this blog

Gotchas with DBCP

A note on Java's Calendar set() method

The mysterious ORA-03111 error