- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
rotate n xs = b ++ a
where n' = n `mod` (length xs)
(a, b) = splitAt ((length xs) - n') xs
rotateAmount xs = _ra 0 ((length xs) - 1) (listArray (0, ((length xs) - 1)) xs)
where _ra s e ys = if (e - s) == 1
then (if ((ys ! s) < (ys ! e)) then s else e) -- base case
else let h = ys ! s -- first item
l = ys ! e -- last item
mi = s + ((e - s) `div` 2) -- middle index
m = ys ! mi -- middle item
in if (h < l)
then s -- return start index
else if (h > m)
then _ra s mi ys
else _ra mi e ys
A “rotated array” is an array of integers in ascending order, after which for every element i, it has been moved to element (i + n) mod sizeOfList. Write a function that takes a rotated array and, in less-than-linear time, returns n (the amount of rotation). http://techguyinmidtown.com/2008/07/05/my-answers-to-the-microsoft-interview-questions