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:
- 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 Fibonacci 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 of the above approach
#include <bits/stdc++.h>
using namespace std;
// 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 Code
int main()
{
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)
cout << "Found at index: " << ind;
else
cout << x << " isn't present in the array";
return 0;
}
// This code is contributed by code_hunt.
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.
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>
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
?>
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 greater 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:

Auxiliary Space: O(1)
References:
https://en.wikipedia.org/wiki/Fibonacci_search_technique
Approach 2: Iterative implementation
Fibonacci Search is a searching algorithm used to find the position of an element in a sorted array. The basic idea behind Fibonacci Search is to use Fibonacci numbers to determine the split points in the array and perform binary search on the appropriate subarray.
Here’s a Python implementation of Fibonacci Search using an iterative approach:
C++
// C++ code for above approach
#include <iostream>
using namespace std;
int fibonacciSearch(int arr[], int n, int x) {
if (n == 0) {
return -1;
}
// Initialize Fibonacci numbers
int fib1 = 0, fib2 = 1, fib3 = fib1 + fib2;
// Find the smallest Fibonacci number greater than or equal to n
while (fib3 < n) {
fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
// Initialize variables for the current and previous split points
int offset = -1;
while (fib3 > 1) {
int i = min(offset + fib2, n-1);
// If x is greater than the value at index i, move the split point to the right
if (arr[i] < x) {
fib3 = fib2;
fib2 = fib1;
fib1 = fib3 - fib2;
offset = i;
}
// If x is less than the value at index i, move the split point to the left
else if (arr[i] > x) {
fib3 = fib1;
fib2 = fib2 - fib1;
fib1 = fib3 - fib2;
}
// If x is equal to the value at index i, return the index
else {
return i;
}
}
// If x is not found in the array, return -1
if (fib2 == 1 && arr[offset+1] == x) {
return offset + 1;
} else {
return -1;
}
}
// Driver code
int main() {
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 = fibonacciSearch(arr, n, x);
if (ind >= 0) {
cout << "Found at index: " << ind << endl;
} else {
cout << x << " isn't present in the array" << endl;
}
return 0;
}
Java
import java.util.*;
class Main {
public static int fibonacciSearch(int[] arr, int x) {
int n = arr.length;
if (n == 0) {
return -1;
}
// Initialize Fibonacci numbers
int fib1 = 0, fib2 = 1, fib3 = fib1 + fib2;
// Find the smallest Fibonacci number greater than or equal to n
while (fib3 < n) {
fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
// Initialize variables for the current and previous split points
int offset = -1;
while (fib3 > 1) {
int i = Math.min(offset + fib2, n-1);
// If x is greater than the value at index i, move the split point to the right
if (arr[i] < x) {
fib3 = fib2;
fib2 = fib1;
fib1 = fib3 - fib2;
offset = i;
}
// If x is less than the value at index i, move the split point to the left
else if (arr[i] > x) {
fib3 = fib1;
fib2 = fib2 - fib1;
fib1 = fib3 - fib2;
}
// If x is equal to the value at index i, return the index
else {
return i;
}
}
// If x is not found in the array, return -1
if (fib2 == 1 && arr[offset+1] == x) {
return offset + 1;
} else {
return -1;
}
}
public static void main(String[] args) {
int[] arr = {10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100, 235};
int n = arr.length;
int x = 235;
int ind = fibonacciSearch(arr, x);
if (ind >= 0) {
System.out.println("Found at index: " + ind);
} else {
System.out.println(x + " isn't present in the array");
}
}
}
Python
def fibonacci_search(arr, x):
n = len(arr)
if n == 0:
return -1
# Initialize Fibonacci numbers
fib1, fib2 = 0, 1
fib3 = fib1 + fib2
# Find the smallest Fibonacci number greater than or equal to n
while fib3 < n:
fib1, fib2 = fib2, fib3
fib3 = fib1 + fib2
# Initialize variables for the current and previous split points
offset = -1
while fib3 > 1:
i = min(offset + fib2, n-1)
# If x is greater than the value at index i, move the split point to the right
if arr[i] < x:
fib3 = fib2
fib2 = fib1
fib1 = fib3 - fib2
offset = i
# If x is less than the value at index i, move the split point to the left
elif arr[i] > x:
fib3 = fib1
fib2 = fib2 - fib1
fib1 = fib3 - fib2
# If x is equal to the value at index i, return the index
else:
return i
# If x is not found in the array, return -1
if fib2 == 1 and arr[offset+1] == x:
return offset + 1
else:
return -1
# Driver Code
arr = [10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100,235]
n = len(arr)
x = 235
ind = fibonacci_search(arr, x)
if ind>=0:
print("Found at index:",ind)
else:
print(x,"isn't present in the array");
# This code is contributed by ajay singh
C#
using System;
class GFG {
public static int FibonacciSearch(int[] arr, int x) {
int n = arr.Length;
if (n == 0) {
return -1;
}
// Initialize Fibonacci numbers
int fib1 = 0, fib2 = 1, fib3 = fib1 + fib2;
// Find the smallest Fibonacci number greater than or equal to n
while (fib3 < n) {
fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
// Initialize variables for the current and previous split points
int offset = -1;
while (fib3 > 1) {
int i = Math.Min(offset + fib2, n - 1);
// If x is greater than the value at index i, move the split point to the right
if (arr[i] < x) {
fib3 = fib2;
fib2 = fib1;
fib1 = fib3 - fib2;
offset = i;
}
// If x is less than the value at index i, move the split point to the left
else if (arr[i] > x) {
fib3 = fib1;
fib2 = fib2 - fib1;
fib1 = fib3 - fib2;
}
// If x is equal to the value at index i, return the index
else {
return i;
}
}
// If x is not found in the array, return -1
if (fib2 == 1 && arr[offset + 1] == x) {
return offset + 1;
} else {
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 = arr.Length;
int x = 235;
// function call
int ind = FibonacciSearch(arr, x);
if (ind >= 0) {
Console.WriteLine("Found at index: " + ind);
} else {
Console.WriteLine(x + " isn't present in the array");
}
}
}
JavaScript
// Javascript equivalent
function fibonacci_search(arr, x) {
var n = arr.length;
if (n === 0) {
return -1;
}
// Initialize Fibonacci numbers
var fib1 = 0;
var fib2 = 1;
var fib3 = fib1 + fib2;
// Find the smallest Fibonacci number greater than or equal to n
while (fib3 < n) {
fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
// Initialize variables for the current and previous split points
var offset = -1;
while (fib3 > 1) {
var i = Math.min(offset + fib2, n - 1);
// If x is greater than the value at index i,
// move the split point to the right
if (arr[i] < x) {
fib3 = fib2;
fib2 = fib1;
fib1 = fib3 - fib2;
offset = i;
// If x is less than the value at index i,
// move the split point to the left
} else if (arr[i] > x) {
fib3 = fib1;
fib2 = fib2 - fib1;
fib1 = fib3 - fib2;
// If x is equal to the value at index i, return the index
} else {
return i;
}
}
// If x is not found in the array, return -1
if (fib2 === 1 && arr[offset + 1] === x) {
return offset + 1;
} else {
return -1;
}
}
// Driver Code
var arr = [10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100,235];
var n = arr.length;
var x = 235;
var ind = fibonacci_search(arr, x);
if (ind >= 0) {
console.log("Found at index:", ind);
} else {
console.log(x,"isn't present in the array");
}
The time complexity of Fibonacci Search is O(log n), where n is the length of the input array.
This is because at each iteration of the algorithm, the search range is reduced by a factor of approximately 1/φ, where φ is the golden ratio (φ ≈ 1.618). The number of iterations required to reduce the search range to a single element is approximately logₑ(n), where logₑ denotes the natural logarithm.
Since each iteration of Fibonacci Search requires constant time, the overall time complexity of the algorithm is O(log n). This makes Fibonacci Search a faster algorithm than linear search, but slower than binary search and other logarithmic search algorithms such as interpolation search and exponential search.
Auxiliary Space: O(1)
Similar Reads
How to check if a given number is Fibonacci number?
Given a number ânâ, how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .. Examples :Input : 8Output : YesInput : 34Output : YesInput : 41Output : NoApproach 1:A simple way is to generate Fibonacci numbers until the generated number
15+ min read
Nth Fibonacci Number
Given a positive integer n, the task is to find the nth Fibonacci number. The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21 Example: Inp
15+ min read
C++ Program For Fibonacci Numbers
The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the whole series. In this article, we will learn how to find the nth Fibonacci number in C++. Examples Input: 5Output: 5Ex
5 min read
Python Program for n-th Fibonacci number
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2With seed values F0 = 0 and F1 = 1.Table of Content Python Program for n-th Fibonacci number Using Formula Python Program for n-th Fibonacci number Using RecursionPython Program for n-th
6 min read
Interesting Programming facts about Fibonacci numbers
We know Fibonacci number, Fn = Fn-1 + Fn-2. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, .... . Here are some interesting facts about Fibonacci number : 1. Pattern in Last digits of Fibonacci numbers : Last digits of first few Fibonacci Numbers ar
15+ min read
Find nth Fibonacci number using Golden ratio
Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ........Different methods to find nth Fibonacci number are already discussed. Another simple way of finding nth Fibonacci number is using golden ratio as Fibonacci numbers maintain approximate golden ratio till infinite. Golden ratio: [Tex]\varphi
6 min read
Fast Doubling method to find the Nth Fibonacci number
Given an integer N, the task is to find the N-th Fibonacci numbers.Examples: Input: N = 3 Output: 2 Explanation: F(1) = 1, F(2) = 1 F(3) = F(1) + F(2) = 2 Input: N = 6 Output: 8 Approach: The Matrix Exponentiation Method is already discussed before. The Doubling Method can be seen as an improvement
14 min read
Tail Recursion for Fibonacci
Write a tail recursive function for calculating the n-th Fibonacci number. Examples : Input : n = 4 Output : fib(4) = 3 Input : n = 9 Output : fib(9) = 34 Prerequisites : Tail Recursion, Fibonacci numbersA recursive function is tail recursive when the recursive call is the last thing executed by the
4 min read
Sum of Fibonacci Numbers
Given a number positive number n, find value of f0 + f1 + f2 + .... + fn where fi indicates i'th Fibonacci number. Remember that f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, ... Examples : Input : n = 3Output : 4Explanation : 0 + 1 + 1 + 2 = 4 Input : n = 4Output : 7Explanation : 0 + 1 + 1 + 2 +
12 min read
Fibonacci Series
Program to Print Fibonacci Series
Ever wondered about the cool math behind the Fibonacci series? This simple pattern has a remarkable presence in nature, from the arrangement of leaves on plants to the spirals of seashells. We're diving into this Fibonacci Series sequence. It's not just math, it's in art, nature, and more! Let's dis
9 min read
Program to Print Fibonacci Series in Java
The Fibonacci series is a series of elements where, the previous two elements are added to get the next element, starting with 0 and 1. In this article, we will learn how to print Fibonacci Series in Java up to the N term, where N is the given number. Examples of Fibonacci Series in JavaInput: N = 1
5 min read
Python Program to Print the Fibonacci sequence
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation. Fn = Fn-1 + Fn-2 with seed values : F0 = 0 and F1 = 1. Table of Content F
3 min read
C Program to Print Fibonacci Series
The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series. Example Input: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.
4 min read
JavaScript Program to print Fibonacci Series
The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. The recurrence relation defines the sequence Fn of Fibonacci numbers: Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1 Examples: Input :
4 min read
Length of longest subsequence of Fibonacci Numbers in an Array
Given an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9,
5 min read
Last digit of sum of numbers in the given range in the Fibonacci series
Given two non-negative integers M, N which signifies the range [M, N] where M ? N, the task is to find the last digit of the sum of FM + FM+1... + FN where FK is the Kth Fibonacci number in the Fibonacci series. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Examples: Input: M = 3, N = 9 Output:
5 min read
K- Fibonacci series
Given integers 'K' and 'N', the task is to find the Nth term of the K-Fibonacci series. In K - Fibonacci series, the first 'K' terms will be '1' and after that every ith term of the series will be the sum of previous 'K' elements in the same series. Examples: Input: N = 4, K = 2 Output: 3 The K-Fibo
7 min read
Fibonacci Series in Bash
Prerequisite: Fibonacci Series Write a program to print the Fibonacci sequence up to nth digit using Bash. Examples: Input : 5 Output : Fibonacci Series is : 0 1 1 2 3 Input :4 Output : Fibonacci Series is : 0 1 1 2 The Fibonacci numbers are the numbers in the following integer sequence . 0, 1, 1, 2
1 min read
R Program to Print the Fibonacci Sequence
The Fibonacci sequence is a series of numbers in which each number (known as a Fibonacci number) is the sum of the two preceding ones. The sequence starts with 0 and 1, and then each subsequent number is the sum of the two previous numbers. The Fibonacci sequence has many applications in various fie
3 min read