Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Searching Algorithms
  • MCQs on Searching Algorithms
  • Tutorial on Searching Algorithms
  • Linear Search
  • Binary Search
  • Ternary Search
  • Jump Search
  • Sentinel Linear Search
  • Interpolation Search
  • Exponential Search
  • Fibonacci Search
  • Ubiquitous Binary Search
  • Linear Search Vs Binary Search
  • Interpolation Search Vs Binary Search
  • Binary Search Vs Ternary Search
  • Sentinel Linear Search Vs Linear Search
Open In App
Next Article:
Fibonacci Search
Next article icon

Fibonacci Search

Last Updated : 29 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a sorted array arr[] of size n and an integer x. Your task is to check if the integer x is present in the array arr[] or not. 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
Explanation: 10 is present at index 3.

Input:  arr[] = [2, 3, 4, 10, 40], x = 11
Output:  -1
Explanation: 11 is not present in the given array.

What is Fibonacci Search?

Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array.

Similarities of Fibonacci Search with Binary Search 

  1. Works for sorted arrays
  2. A Divide and Conquer Algorithm.
  3. Has Log n time complexity.

Differences of Fibonacci Search with Binary Search

  1. Fibonacci Search divides given array into unequal parts
  2. 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.
  3. 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, ...

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

F(n - 2) ≈ (1/3)*F(n) and (Note : This is an approximation, not equal)
F(n - 1) ≈ (2/3)*F(n).

Fibonacci Search Algorithm - O(log n) Time and O(1) Space

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. 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.

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

  1. 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].
  2. While the array has elements to be inspected: 
    1. Compare x with the last element of the range covered by fibMm2 (variable a in the below code).
    2. If x matches, return index
    3. 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.
    4. 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.
  3. 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++
#include <bits/stdc++.h> using namespace std;   // Returns index of x if present, else returns -1 int search(vector<int> &arr, int x) {     int n = arr.size();      // initialize first three fibonacci numbers     int a =  0, b = 1, c = 1;       // iterate while c is smaller than n     // c stores the smallest Fibonacci      // number greater than or equal to n     while (c < n) {         a = b;         b = c;         c = a + b;     }       // marks the eliminated range from front     int offset = -1;       // while there are elements to be inspected     // Note that we compare arr[a] with x.      // When c becomes 1, a becomes 08     while (c > 1) {          // check if a is a valid location         int i = min(offset + a, n - 1);           // if x is greater than the value at index a,         // cut the subarray array from offset to i          if (arr[i] < x) {             c = b;             b = a;             a = c - b;             offset = i;         }           // else if x is greater than the value at          // index a,cut the subarray after i+1         else if (arr[i] > x) {             c = a;             b = b - a;             a = c - b;         }           // else if element found, return index         else             return i;     }       // comparing the last element with x     if (b && arr[offset + 1] == x)         return offset + 1;       // element not found, return -1     return -1; }  int main() {     vector<int> arr = {2, 3, 4, 10, 40};     int x = 10;     cout << search(arr, x);     return 0; } 
Java
import java.io.*;  class GfG {      // Returns index of x if present, else returns -1     public static int search(int[] arr, int x) {         int n = arr.length;          // initialize first three fibonacci numbers         int a =  0, b = 1, c = 1;           // iterate while c is smaller than n         // c stores the smallest Fibonacci          // number greater than or equal to n         while (c < n) {             a = b;             b = c;             c = a + b;         }           // marks the eliminated range from front         int offset = -1;           // while there are elements to be inspected         // Note that we compare arr[a] with x.          // When c becomes 1, a becomes 08         while (c > 1) {              // check if a is a valid location             int i = Math.min(offset + a, n - 1);               // if x is greater than the value at index a,             // cut the subarray array from offset to i              if (arr[i] < x) {                 c = b;                 b = a;                 a = c - b;                 offset = i;             }               // else if x is greater than the value at              // index a,cut the subarray after i+1             else if (arr[i] > x) {                 c = a;                 b = b - a;                 a = c - b;             }               // else if element found, return index             else                 return i;         }           // comparing the last element with x         if (b != 0 && arr[offset + 1] == x)             return offset + 1;           // element not found, return -1         return -1;     }      public static void main(String[] args) {         int[] arr = {2, 3, 4, 10, 40};         int x = 10;         System.out.println(search(arr, x));     } } 
Python
#!/usr/bin/env python3  # Returns index of x if present, else returns -1 def search(arr, x):     n = len(arr)      # initialize first three fibonacci numbers     a =  0     b = 1     c = 1       # iterate while c is smaller than n     # c stores the smallest Fibonacci      # number greater than or equal to n     while c < n:         a = b         b = c         c = a + b       # marks the eliminated range from front     offset = -1       # while there are elements to be inspected     # Note that we compare arr[a] with x.      # When c becomes 1, a becomes 08     while c > 1:          # check if a is a valid location         i = min(offset + a, n - 1)           # if x is greater than the value at index a,         # cut the subarray array from offset to i          if arr[i] < x:             c = b             b = a             a = c - b             offset = i           # else if x is greater than the value at          # index a,cut the subarray after i+1         elif arr[i] > x:             c = a             b = b - a             a = c - b           # else if element found, return index         else:             return i       # comparing the last element with x     if b and arr[offset + 1] == x:         return offset + 1       # element not found, return -1     return -1   if __name__ == "__main__":     arr = [2, 3, 4, 10, 40]     x = 10     print(search(arr, x)) 
C#
using System;  class GfG {      // Returns index of x if present, else returns -1     public static int search(int[] arr, int x) {         int n = arr.Length;          // initialize first three fibonacci numbers         int a =  0, b = 1, c = 1;           // iterate while c is smaller than n         // c stores the smallest Fibonacci          // number greater than or equal to n         while (c < n) {             a = b;             b = c;             c = a + b;         }           // marks the eliminated range from front         int offset = -1;           // while there are elements to be inspected         // Note that we compare arr[a] with x.          // When c becomes 1, a becomes 08         while (c > 1) {              // check if a is a valid location             int i = Math.Min(offset + a, n - 1);               // if x is greater than the value at index a,             // cut the subarray array from offset to i              if (arr[i] < x) {                 c = b;                 b = a;                 a = c - b;                 offset = i;             }               // else if x is greater than the value at              // index a,cut the subarray after i+1             else if (arr[i] > x) {                 c = a;                 b = b - a;                 a = c - b;             }               // else if element found, return index             else                 return i;         }           // comparing the last element with x         if (b != 0 && arr[offset + 1] == x)             return offset + 1;           // element not found, return -1         return -1;     }      public static void Main() {         int[] arr = {2, 3, 4, 10, 40};         int x = 10;         Console.WriteLine(search(arr, x));     } } 
JavaScript
// Returns index of x if present, else returns -1 function search(arr, x) {     let n = arr.length;      // initialize first three fibonacci numbers     let a =  0, b = 1, c = 1;       // iterate while c is smaller than n     // c stores the smallest Fibonacci      // number greater than or equal to n     while (c < n) {         a = b;         b = c;         c = a + b;     }       // marks the eliminated range from front     let offset = -1;       // while there are elements to be inspected     // Note that we compare arr[a] with x.      // When c becomes 1, a becomes 08     while (c > 1) {          // check if a is a valid location         let i = Math.min(offset + a, n - 1);           // if x is greater than the value at index a,         // cut the subarray array from offset to i          if (arr[i] < x) {             c = b;             b = a;             a = c - b;             offset = i;         }           // else if x is greater than the value at          // index a,cut the subarray after i+1         else if (arr[i] > x) {             c = a;             b = b - a;             a = c - b;         }           // else if element found, return index         else             return i;     }       // comparing the last element with x     if (b && arr[offset + 1] === x)         return offset + 1;       // element not found, return -1     return -1; }  let arr = [2, 3, 4, 10, 40]; let x = 10; console.log(search(arr, x)); 

Output
3

Illustration: 

Let us understand the algorithm with the below example:  

pic

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). 

fibSearch

Visualization:  

fibSearch3

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: 

fibSearch2

Auxiliary Space: O(1)


Next Article
Fibonacci Search

K

kartik
Improve
Article Tags :
  • Searching
  • DSA
  • Fibonacci
Practice Tags :
  • Fibonacci
  • Searching

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, 21Example:Input:
    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 they are used to generate the entire series.Examples:Input: 5Output: 5Explanation: As 5 is the 5th Fibonacci number of series 0, 1, 1, 2, 3, 5
    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 ContentPython 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: \varphi ={\fr
    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 = 4Input : n = 4Output : 7Explanation : 0 + 1 + 1 + 2 + 3
    9 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
    8 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 generate the next term. It starts with 0 and 1, for example, 0, 1, 1, 2, 3, and so on. We can mathematically represent it in the form of a function to generate the n'th Fibonacci number because it follows a con
    5 min read
    Print the Fibonacci sequence - Python
    To print the Fibonacci sequence in Python, we need to generate a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The Fibonacci sequence follows a specific pattern that begins with 0 and 1, and every subsequent number is the sum of the two previous num
    5 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.ExampleInput: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.In
    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 = 1Examples:Input : 5
    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
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences