Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

3 sequential numbers

Name: eye 2012-04-04 16:06

Alright geniuses, if you think your programming ability is up to par, lets see you solve this:

Write an algorithm which in O(n) time finds 3 indices i, j, and k such that i < j < k and array[i] < array[j] < array[k].

Name: Anonymous 2012-04-05 10:11

More explanation:

[5, 6, 1, 2, 3] → 1,2,3  because we reset x to 1.
[1, 5, 2, 3] → 1,2,3, because we finish at [2,3] and then unshift 1 back to the front of our tuple.
[5, 6, 1, 2, 7] → 1,2,3, because we reset x to 1. Because 5,6 OR 1,2 are valid preceeding numbers for 7.
[5, 6, 1, 2, 7, 8] → 1,2,7, because we reset at 1 and finish at 7.
[1, 2, 999, 3] → 1,2,999
[999, 1, 2, 3] → [1,2,3] because we reset at 1
[11,12,8,9,5,6,3,4,1,2,3] → [1,2,3] because we reset at 8, 5, 3, 1.

I just realised that I should throw an error at the unshift line to ensure I don't unshift larger numbers. It'd be an invalid list anyway, but for the sake of helpfulness to the programmer.

Not sure I can come up with a proof for this, I suck at proofs. Anyone want to make a proof for me?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List