1. Fibonacci state
Track consecutive Fibonacci numbers.
let fib2 = 0Typorithm · hard
Step 1 of 8 · intro card
Search Algorithms
Fibonacci Search narrows a sorted array using Fibonacci-sized probe positions.
Fibonacci Search is another sorted-array search strategy.
It first finds a Fibonacci number large enough to cover the array.
The offset marks the part already eliminated from the left.
Each comparison shrinks the Fibonacci window.
It is logarithmic and historically useful where division was expensive.
Requires sorted input.
Use Fibonacci window sizes.
Track eliminated left side with offset.
Probe at offset + fib2.
Time is O(log n).
function fibonacciSearch(arr, target) {
let fib2 = 0;
let fib1 = 1;
let fib = fib1 + fib2;
while (fib < arr.length) {
fib2 = fib1;
fib1 = fib;
fib = fib1 + fib2;
}
let offset = -1;
while (fib > 1) {
const i = Math.min(offset + fib2, arr.length - 1);
if (arr[i] < target) {
fib = fib1;
fib1 = fib2;
fib2 = fib - fib1;
offset = i;
} else if (arr[i] > target) {
fib = fib2;
fib1 = fib1 - fib2;
fib2 = fib - fib1;
} else {
return i;
}
}
if (fib1 && arr[offset + 1] === target) return offset + 1;
return -1;
}Connect each part of the code to the concept before you type.
Track consecutive Fibonacci numbers.
let fib2 = 0Find the smallest Fibonacci number covering the array length.
while (fib < arr.length)offset marks the eliminated left side.
let offset = -1Probe using a Fibonacci-sized step from the offset.
const i = Math.min(offset + fib2, arr.length - 1)If the probe is too small, eliminate everything through i.
offset = iReturn the probe index on a match.
return iCheck the final remaining candidate.
if (fib1 && arr[offset + 1] === target) return offset + 1Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputArray [1,3,5,7,9,11,13,15] · target 9
Follow temporary values, local state and supporting structures at this step.
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state