# Fibonacci Search

Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is present in array else return -1.

Examples:

Input: arr[] = {2, 3, 4, 10, 40}, x = 10 Output: 3 Element x is present at index 3. Input: arr[] = {2, 3, 4, 10, 40}, x = 11 Output: -1 Element x is not present.

Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array.**Similarities with Binary Search:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

- Works for sorted arrays
- A Divide and Conquer Algorithm.
- Has Log n time complexity.

**Differences with Binary Search**:

- Fibonacci Search divides given array into unequal parts
- Binary Search uses a division operator to divide range. Fibonacci Search doesn’t use /, but uses + and -. The division operator may be costly on some CPUs.
- Fibonacci Search examines relatively closer elements in subsequent steps. So when the input array is big that cannot fit in CPU cache or even in RAM, Fibonacci Search can be useful.

**Background:**

Fibonacci Numbers are recursively defined as F(n) = F(n-1) + F(n-2), F(0) = 0, F(1) = 1. First few Fibinacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …**Observations:**

Below observation is used for range elimination, and hence for the O(log(n)) complexity.

F(n - 2) ≈ (1/3)*F(n) and F(n - 1) ≈ (2/3)*F(n).

**Algorithm:**

Let the searched element be x.

The idea is to first find the smallest Fibonacci number that is greater than or equal to the length of the given array. Let the found Fibonacci number be fib (m’th Fibonacci number). We use (m-2)’th Fibonacci number as the index (If it is a valid index). Let (m-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is same, we return i. Else if x is greater, we recur for subarray after i, else we recur for subarray before i.

Below is the complete algorithm

Let arr[0..n-1] be the input array and the element to be searched be x.

- Find the smallest Fibonacci Number greater than or equal to n. Let this number be fibM [mâ€™th Fibonacci Number]. Let the two Fibonacci numbers preceding it be fibMm1 [(m-1)â€™th Fibonacci Number] and fibMm2 [(m-2)â€™th Fibonacci Number].
- While the array has elements to be inspected:
- Compare x with the last element of the range covered by fibMm2
**If**x matches, return index**Else If**x is less than the element, move the three Fibonacci variables two Fibonacci down, indicating elimination of approximately rear two-third of the remaining array.**Else**x is greater than the element, move the three Fibonacci variables one Fibonacci down. Reset offset to index. Together these indicate the elimination of approximately front one-third of the remaining array.

- Since there might be a single element remaining for comparison, check if fibMm1 is 1. If Yes, compare x with that remaining element. If match, return index.

## C

`// C program for Fibonacci Search` `#include <stdio.h>` `// Utility function to find minimum of two elements` `int` `min(` `int` `x, ` `int` `y) { ` `return` `(x <= y) ? x : y; }` `/* Returns index of x if present, else returns -1 */` `int` `fibMonaccianSearch(` `int` `arr[], ` `int` `x, ` `int` `n)` `{` ` ` `/* Initialize fibonacci numbers */` ` ` `int` `fibMMm2 = 0; ` `// (m-2)'th Fibonacci No.` ` ` `int` `fibMMm1 = 1; ` `// (m-1)'th Fibonacci No.` ` ` `int` `fibM = fibMMm2 + fibMMm1; ` `// m'th Fibonacci` ` ` `/* fibM is going to store the smallest Fibonacci` ` ` `Number greater than or equal to n */` ` ` `while` `(fibM < n) {` ` ` `fibMMm2 = fibMMm1;` ` ` `fibMMm1 = fibM;` ` ` `fibM = fibMMm2 + fibMMm1;` ` ` `}` ` ` `// Marks the eliminated range from front` ` ` `int` `offset = -1;` ` ` `/* while there are elements to be inspected. Note that` ` ` `we compare arr[fibMm2] with x. When fibM becomes 1,` ` ` `fibMm2 becomes 0 */` ` ` `while` `(fibM > 1) {` ` ` `// Check if fibMm2 is a valid location` ` ` `int` `i = min(offset + fibMMm2, n - 1);` ` ` `/* If x is greater than the value at index fibMm2,` ` ` `cut the subarray array from offset to i */` ` ` `if` `(arr[i] < x) {` ` ` `fibM = fibMMm1;` ` ` `fibMMm1 = fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `offset = i;` ` ` `}` ` ` `/* If x is greater than the value at index fibMm2,` ` ` `cut the subarray after i+1 */` ` ` `else` `if` `(arr[i] > x) {` ` ` `fibM = fibMMm2;` ` ` `fibMMm1 = fibMMm1 - fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `}` ` ` `/* element found. return index */` ` ` `else` ` ` `return` `i;` ` ` `}` ` ` `/* comparing the last element with x */` ` ` `if` `(fibMMm1 && arr[offset + 1] == x)` ` ` `return` `offset + 1;` ` ` `/*element not found. return -1 */` ` ` `return` `-1;` `}` `/* driver function */` `int` `main(` `void` `)` `{` ` ` `int` `arr[]` ` ` `= { 10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100,235};` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `x = 235;` ` ` `int` `ind = fibMonaccianSearch(arr, x, n);` ` ` `if` `(ind>=0)` ` ` `printf` `(` `"Found at index: %d"` `,ind);` ` ` `else` ` ` `printf` `(` `"%d isn't present in the array"` `,x);` ` ` `return` `0;` `}` |

## Java

`// Java program for Fibonacci Search` `import` `java.util.*;` `class` `Fibonacci {` ` ` `// Utility function to find minimum` ` ` `// of two elements` ` ` `public` `static` `int` `min(` `int` `x, ` `int` `y)` ` ` `{` ` ` `return` `(x <= y) ? x : y;` ` ` `}` ` ` `/* Returns index of x if present, else returns -1 */` ` ` `public` `static` `int` `fibMonaccianSearch(` `int` `arr[], ` `int` `x,` ` ` `int` `n)` ` ` `{` ` ` `/* Initialize fibonacci numbers */` ` ` `int` `fibMMm2 = ` `0` `; ` `// (m-2)'th Fibonacci No.` ` ` `int` `fibMMm1 = ` `1` `; ` `// (m-1)'th Fibonacci No.` ` ` `int` `fibM = fibMMm2 + fibMMm1; ` `// m'th Fibonacci` ` ` `/* fibM is going to store the smallest` ` ` `Fibonacci Number greater than or equal to n */` ` ` `while` `(fibM < n) {` ` ` `fibMMm2 = fibMMm1;` ` ` `fibMMm1 = fibM;` ` ` `fibM = fibMMm2 + fibMMm1;` ` ` `}` ` ` `// Marks the eliminated range from front` ` ` `int` `offset = -` `1` `;` ` ` `/* while there are elements to be inspected.` ` ` `Note that we compare arr[fibMm2] with x.` ` ` `When fibM becomes 1, fibMm2 becomes 0 */` ` ` `while` `(fibM > ` `1` `) {` ` ` `// Check if fibMm2 is a valid location` ` ` `int` `i = min(offset + fibMMm2, n - ` `1` `);` ` ` `/* If x is greater than the value at` ` ` `index fibMm2, cut the subarray array` ` ` `from offset to i */` ` ` `if` `(arr[i] < x) {` ` ` `fibM = fibMMm1;` ` ` `fibMMm1 = fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `offset = i;` ` ` `}` ` ` `/* If x is less than the value at index` ` ` `fibMm2, cut the subarray after i+1 */` ` ` `else` `if` `(arr[i] > x) {` ` ` `fibM = fibMMm2;` ` ` `fibMMm1 = fibMMm1 - fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `}` ` ` `/* element found. return index */` ` ` `else` ` ` `return` `i;` ` ` `}` ` ` `/* comparing the last element with x */` ` ` `if` `(fibMMm1 == ` `1` `&& arr[n-` `1` `] == x)` ` ` `return` `n-` `1` `;` ` ` `/*element not found. return -1 */` ` ` `return` `-` `1` `;` ` ` `}` ` ` `// driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `arr[] = { ` `10` `, ` `22` `, ` `35` `, ` `40` `, ` `45` `, ` `50` `,` ` ` `80` `, ` `82` `, ` `85` `, ` `90` `, ` `100` `,` `235` `};` ` ` `int` `n = ` `12` `;` ` ` `int` `x = ` `235` `;` ` ` `int` `ind = fibMonaccianSearch(arr, x, n);` ` ` `if` `(ind>=` `0` `)` ` ` `System.out.print(` `"Found at index: "` ` ` `+ind);` ` ` `else` ` ` `System.out.print(x+` `" isn't present in the array"` `);` ` ` `}` `}` `// This code is contributed by rishabh_jain` |

## Python3

`# Python3 program for Fibonacci search.` `from` `bisect ` `import` `bisect_left` `# Returns index of x if present, else` `# returns -1` `def` `fibMonaccianSearch(arr, x, n):` ` ` `# Initialize fibonacci numbers` ` ` `fibMMm2 ` `=` `0` `# (m-2)'th Fibonacci No.` ` ` `fibMMm1 ` `=` `1` `# (m-1)'th Fibonacci No.` ` ` `fibM ` `=` `fibMMm2 ` `+` `fibMMm1 ` `# m'th Fibonacci` ` ` `# fibM is going to store the smallest` ` ` `# Fibonacci Number greater than or equal to n` ` ` `while` `(fibM < n):` ` ` `fibMMm2 ` `=` `fibMMm1` ` ` `fibMMm1 ` `=` `fibM` ` ` `fibM ` `=` `fibMMm2 ` `+` `fibMMm1` ` ` `# Marks the eliminated range from front` ` ` `offset ` `=` `-` `1` ` ` `# while there are elements to be inspected.` ` ` `# Note that we compare arr[fibMm2] with x.` ` ` `# When fibM becomes 1, fibMm2 becomes 0` ` ` `while` `(fibM > ` `1` `):` ` ` `# Check if fibMm2 is a valid location` ` ` `i ` `=` `min` `(offset` `+` `fibMMm2, n` `-` `1` `)` ` ` `# If x is greater than the value at` ` ` `# index fibMm2, cut the subarray array` ` ` `# from offset to i` ` ` `if` `(arr[i] < x):` ` ` `fibM ` `=` `fibMMm1` ` ` `fibMMm1 ` `=` `fibMMm2` ` ` `fibMMm2 ` `=` `fibM ` `-` `fibMMm1` ` ` `offset ` `=` `i` ` ` `# If x is less than the value at` ` ` `# index fibMm2, cut the subarray` ` ` `# after i+1` ` ` `elif` `(arr[i] > x):` ` ` `fibM ` `=` `fibMMm2` ` ` `fibMMm1 ` `=` `fibMMm1 ` `-` `fibMMm2` ` ` `fibMMm2 ` `=` `fibM ` `-` `fibMMm1` ` ` `# element found. return index` ` ` `else` `:` ` ` `return` `i` ` ` `# comparing the last element with x */` ` ` `if` `(fibMMm1 ` `and` `arr[n` `-` `1` `] ` `=` `=` `x):` ` ` `return` `n` `-` `1` ` ` `# element not found. return -1` ` ` `return` `-` `1` `# Driver Code` `arr ` `=` `[` `10` `, ` `22` `, ` `35` `, ` `40` `, ` `45` `, ` `50` `,` ` ` `80` `, ` `82` `, ` `85` `, ` `90` `, ` `100` `,` `235` `]` `n ` `=` `len` `(arr)` `x ` `=` `235` `ind ` `=` `fibMonaccianSearch(arr, x, n)` `if` `ind>` `=` `0` `:` ` ` `print` `(` `"Found at index:"` `,ind)` `else` `:` ` ` `print` `(x,` `"isn't present in the array"` `);` `# This code is contributed by rishabh_jain` |

## C#

`// C# program for Fibonacci Search` `using` `System;` `class` `GFG {` ` ` `// Utility function to find minimum` ` ` `// of two elements` ` ` `public` `static` `int` `min(` `int` `x, ` `int` `y)` ` ` `{` ` ` `return` `(x <= y) ? x : y;` ` ` `}` ` ` `/* Returns index of x if present, else returns -1 */` ` ` `public` `static` `int` `fibMonaccianSearch(` `int` `[] arr, ` `int` `x,` ` ` `int` `n)` ` ` `{` ` ` `/* Initialize fibonacci numbers */` ` ` `int` `fibMMm2 = 0; ` `// (m-2)'th Fibonacci No.` ` ` `int` `fibMMm1 = 1; ` `// (m-1)'th Fibonacci No.` ` ` `int` `fibM = fibMMm2 + fibMMm1; ` `// m'th Fibonacci` ` ` `/* fibM is going to store the smallest` ` ` `Fibonacci Number greater than or equal to n */` ` ` `while` `(fibM < n) {` ` ` `fibMMm2 = fibMMm1;` ` ` `fibMMm1 = fibM;` ` ` `fibM = fibMMm2 + fibMMm1;` ` ` `}` ` ` `// Marks the eliminated range from front` ` ` `int` `offset = -1;` ` ` `/* while there are elements to be inspected.` ` ` `Note that we compare arr[fibMm2] with x.` ` ` `When fibM becomes 1, fibMm2 becomes 0 */` ` ` `while` `(fibM > 1) {` ` ` `// Check if fibMm2 is a valid location` ` ` `int` `i = min(offset + fibMMm2, n - 1);` ` ` `/* If x is greater than the value at` ` ` `index fibMm2, cut the subarray array` ` ` `from offset to i */` ` ` `if` `(arr[i] < x) {` ` ` `fibM = fibMMm1;` ` ` `fibMMm1 = fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `offset = i;` ` ` `}` ` ` `/* If x is less than the value at index` ` ` `fibMm2, cut the subarray after i+1 */` ` ` `else` `if` `(arr[i] > x) {` ` ` `fibM = fibMMm2;` ` ` `fibMMm1 = fibMMm1 - fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `}` ` ` `/* element found. return index */` ` ` `else` ` ` `return` `i;` ` ` `}` ` ` `/* comparing the last element with x */` ` ` `if` `(fibMMm1 == 1 && arr[n-1] == x)` ` ` `return` `n-1;` ` ` `/*element not found. return -1 */` ` ` `return` `-1;` ` ` `}` ` ` `// driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `[] arr = { 10, 22, 35, 40, 45, 50,` ` ` `80, 82, 85, 90, 100,235 };` ` ` `int` `n = 12;` ` ` `int` `x = 235;` ` ` `int` `ind = fibMonaccianSearch(arr, x, n);` ` ` `if` `(ind>=0)` ` ` `Console.Write(` `"Found at index: "` `+ind);` ` ` `else` ` ` `Console.Write(x+` `" isn't present in the array"` `);` ` ` `}` `}` `// This code is contributed by nitin mittal.` |

## PHP

`<?php` `// PHP program for Fibonacci Search` `/* Returns index of x if present, else returns -1 */` `function` `fibMonaccianSearch(` `$arr` `, ` `$x` `, ` `$n` `)` `{` ` ` `/* Initialize fibonacci numbers */` ` ` `$fibMMm2` `= 0; ` `// (m-2)'th Fibonacci No.` ` ` `$fibMMm1` `= 1; ` `// (m-1)'th Fibonacci No.` ` ` `$fibM` `= ` `$fibMMm2` `+ ` `$fibMMm1` `; ` `// m'th Fibonacci` ` ` `/* fibM is going to store the smallest Fibonacci` ` ` `Number greater than or equal to n */` ` ` `while` `(` `$fibM` `< ` `$n` `)` ` ` `{` ` ` `$fibMMm2` `= ` `$fibMMm1` `;` ` ` `$fibMMm1` `= ` `$fibM` `;` ` ` `$fibM` `= ` `$fibMMm2` `+ ` `$fibMMm1` `;` ` ` `}` ` ` `// Marks the eliminated range from front` ` ` `$offset` `= -1;` ` ` `/* while there are elements to be inspected. Note that` ` ` `we compare arr[fibMm2] with x. When fibM becomes 1,` ` ` `fibMm2 becomes 0 */` ` ` `while` `(` `$fibM` `> 1)` ` ` `{` ` ` `// Check if fibMm2 is a valid location` ` ` `$i` `= min(` `$offset` `+` `$fibMMm2` `, ` `$n` `-1);` ` ` `/* If x is greater than the value at index fibMm2,` ` ` `cut the subarray array from offset to i */` ` ` `if` `(` `$arr` `[` `$i` `] < ` `$x` `)` ` ` `{` ` ` `$fibM` `= ` `$fibMMm1` `;` ` ` `$fibMMm1` `= ` `$fibMMm2` `;` ` ` `$fibMMm2` `= ` `$fibM` `- ` `$fibMMm1` `;` ` ` `$offset` `= ` `$i` `;` ` ` `}` ` ` `/* If x is less than the value at index fibMm2,` ` ` `cut the subarray after i+1 */` ` ` `else` `if` `(` `$arr` `[` `$i` `] > ` `$x` `)` ` ` `{` ` ` `$fibM` `= ` `$fibMMm2` `;` ` ` `$fibMMm1` `= ` `$fibMMm1` `- ` `$fibMMm2` `;` ` ` `$fibMMm2` `= ` `$fibM` `- ` `$fibMMm1` `;` ` ` `}` ` ` `/* element found. return index */` ` ` `else` `return` `$i` `;` ` ` `}` ` ` `/* comparing the last element with x */` ` ` `if` `(` `$fibMMm1` `&& ` `$arr` `[` `$n` `-1] == ` `$x` `)` `return` `$n` `-1;` ` ` `/*element not found. return -1 */` ` ` `return` `-1;` `}` `/* driver code */` ` ` `$arr` `= ` `array` `(10, 22, 35, 40, 45, 50, 80, 82,85, 90, 100,235);` ` ` `$n` `= ` `count` `(` `$arr` `);` ` ` `$x` `= 235;` ` ` `$ind` `= fibMonaccianSearch(` `$arr` `, ` `$x` `, ` `$n` `);` ` ` `if` `(` `$ind` `>=0)` ` ` `printf(` `"Found at index: "` `.` `$ind` `);` ` ` `else` ` ` `printf(` `$x` `.` `" isn't present in the array"` `);` `// This code is contributed by mits` `?>` |

## Javascript

`<script>` `// Javascript program for Fibonacci Search` ` ` `/* Returns index of x if present, else returns -1 */` `function` `fibMonaccianSearch(arr, x, n)` `{` ` ` `/* Initialize fibonacci numbers */` ` ` `let fibMMm2 = 0; ` `// (m-2)'th Fibonacci No.` ` ` `let fibMMm1 = 1; ` `// (m-1)'th Fibonacci No.` ` ` `let fibM = fibMMm2 + fibMMm1; ` `// m'th Fibonacci` ` ` ` ` `/* fibM is going to store the smallest Fibonacci` ` ` `Number greater than or equal to n */` ` ` `while` `(fibM < n)` ` ` `{` ` ` `fibMMm2 = fibMMm1;` ` ` `fibMMm1 = fibM;` ` ` `fibM = fibMMm2 + fibMMm1;` ` ` `}` ` ` ` ` `// Marks the eliminated range from front` ` ` `let offset = -1;` ` ` ` ` `/* while there are elements to be inspected. Note that` ` ` `we compare arr[fibMm2] with x. When fibM becomes 1,` ` ` `fibMm2 becomes 0 */` ` ` ` ` `while` `(fibM > 1)` ` ` `{` ` ` `// Check if fibMm2 is a valid location` ` ` `let i = Math.min(offset + fibMMm2, n-1);` ` ` ` ` `/* If x is greater than the value at index fibMm2,` ` ` `cut the subarray array from offset to i */` ` ` `if` `(arr[i] < x)` ` ` `{` ` ` `fibM = fibMMm1;` ` ` `fibMMm1 = fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `offset = i;` ` ` `}` ` ` ` ` `/* If x is less than the value at index fibMm2,` ` ` `cut the subarray after i+1 */` ` ` `else` `if` `(arr[i] > x)` ` ` `{` ` ` `fibM = fibMMm2;` ` ` `fibMMm1 = fibMMm1 - fibMMm2;` ` ` `fibMMm2 = fibM - fibMMm1;` ` ` `}` ` ` ` ` `/* element found. return index */` ` ` `else` `return` `i;` ` ` `}` ` ` ` ` `/* comparing the last element with x */` ` ` `if` `(fibMMm1 && arr[n-1] == x){` ` ` `return` `n-1` ` ` `}` ` ` ` ` `/*element not found. return -1 */` ` ` `return` `-1;` `}` ` ` `/* driver code */` ` ` `let arr = [10, 22, 35, 40, 45, 50, 80, 82,85, 90, 100,235];` ` ` `let n = arr.length;` ` ` `let x = 235;` ` ` `let ind = fibMonaccianSearch(arr, x, n);` ` ` `if` `(ind>=0){` ` ` `document.write(` `"Found at index: "` `+ ind);` ` ` `}` `else` `{` ` ` `document.write(x + ` `" isn't present in the array"` `);` ` ` `}` ` ` `// This code is contributed by _saurabh_jaiswal` `</script>` |

**Output**

Found at index: 11

**Illustration:**

Let us understand the algorithm with the below example:

Illustration assumption: 1-based indexing. Target element x is 85. Length of array n = 11.

Smallest Fibonacci number greate than or equal to 11 is 13. As per our illustration, fibMm2 = 5, fibMm1 = 8, and fibM = 13.

Another implementation detail is the offset variable (zero-initialized). It marks the range that has been eliminated, starting from the front. We will update it from time to time.

Now since the offset value is an index and all indices including it and below it have been eliminated, it only makes sense to add something to it. Since fibMm2 marks approximately one-third of our array, as well as the indices it marks, are sure to be valid ones, we can add fibMm2 to offset and check the element at index i = min(offset + fibMm2, n).

**Visualization:**

**Time Complexity analysis:**

The worst-case will occur when we have our target in the larger (2/3) fraction of the array, as we proceed to find it. In other words, we are eliminating the smaller (1/3) fraction of the array every time. We call once for n, then for(2/3) n, then for (4/9) n, and henceforth.

Consider that:

**References:**

https://en.wikipedia.org/wiki/Fibonacci_search_technique

This article is contributed by **Yash Varyani**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above