Open In App

Length of longest Fibonacci subarray

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of integer elements, the task is to find the length of the largest sub-array of arr[] such that all the elements of the sub-array are Fibonacci numbers.

Examples:

Input: arr[] = {11, 8, 21, 5, 3, 28, 4}
Output: 4
Explanation:
Maximum length sub-array with all elements as Fibonacci number is {8, 21, 5, 3}.

Input: arr[] = {25, 100, 36}
Output: 0

Approach: This problem can be solved by traversing through the array arr[]. Follow the steps below to solve this problem.

  • Initialize variables say, max_length and current_length as 0 to store the maximum length of the sub-array and the current length of the sub-array such that every element in the sub-array is Fibonacci number.
  • Iterate in the range [0, N-1] using the variable i:
    • If the current number is a Fibonacci number then increment current_length by 1, otherwise, set current_length as 0.
    • Now, assign max_length as maximum of the current_length and max_length.
  • After completing the above steps, print max_length as the required answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// A utility function that returns
// true if x is perfect square
bool isPerfectSquare(int x)
{
    int s = sqrt(x);
    return (s * s == x);
}
 
// Returns true if n is a
// Fibonacci Number, else false
bool isFibonacci(int n)
{
    // Here n is Fibinac ci if one of 5*n*n + 4
    // or 5*n*n - 4 or both is a perfect square
    return isPerfectSquare(5 * n * n + 4)
           || isPerfectSquare(5 * n * n - 4);
}
 
// Function to find the length of the
// largest sub-array of an array every
// element of whose is a Fibonacci number
int contiguousFibonacciNumber(int arr[], int n)
{
 
    int current_length = 0;
    int max_length = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
        // Check if arr[i] is a Fibonacci number
        if(isFibonacci(arr[i])) {
            current_length++;
        }
        else{
           current_length = 0;
        }
 
        // Stores the maximum length of the
        // Fibonacci number subarray
        max_length = max(max_length, current_length);
    }
   
    // Finally, return the maximum length
    return max_length;
}
 
// Driver code
int main()
{
 
    // Given Input
    int arr[] = { 11, 8, 21, 5, 3, 28, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << contiguousFibonacciNumber(arr, n);
 
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG
{
 
  // A utility function that returns
  // true if x is perfect square
  public static boolean isPerfectSquare(int x)
  {
    int s =(int) Math.sqrt(x);
    return (s * s == x);
  }
 
  // Returns true if n is a
  // Fibonacci Number, else false
  public static boolean isFibonacci(int n)
  {
 
    // Here n is Fibonacci if one of 5*n*n + 4
    // or 5*n*n - 4 or both is a perfect square
    return isPerfectSquare(5 * n * n + 4)
      || isPerfectSquare(5 * n * n - 4);
  }
 
  // Function to find the length of the
  // largest sub-array of an array every
  // element of whose is a Fibonacci number
  public static int contiguousFibonacciNumber(int arr[], int n)
  {
 
    int current_length = 0;
    int max_length = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
      // Check if arr[i] is a Fibonacci number
      if (isFibonacci(arr[i])) {
        current_length++;
      }
      else {
        current_length = 0;
      }
 
      // Stores the maximum length of the
      // Fibonacci number subarray
      max_length = Math.max(max_length, current_length);
    }
 
    // Finally, return the maximum length
    return max_length;
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    // Given Input
    int arr[] = { 11, 8, 21, 5, 3, 28, 4 };
    int n = arr.length;
 
    // Function Call
    System.out.println( contiguousFibonacciNumber(arr, n));
  }
}
 
 // This code is contributed by Potta Lokesh


Python3




# Python3 program for the above approach
import math
 
# A utility function that returns
# true if x is perfect square
def isPerfectSquare(x):
     
    s = int(math.sqrt(x))
     
    if s * s == x:
        return True
    else:
        return False
 
# Returns true if n is a
# Fibonacci Number, else false
def isFibonacci(n):
     
    # Here n is fibonacci if one of 5*n*n+4
    # or 5*n*n-4 or both is a perfect square
    return (isPerfectSquare(5 * n * n + 4) or
            isPerfectSquare(5 * n * n - 4))
   
# Function to find the length of the
# largest sub-array of an array every
# element of whose is a Fibonacci number
def contiguousFibonacciNumber(arr, n):
     
    current_length = 0
    max_length = 0
     
    # Traverse the array arr
    for i in range(0, n):
         
        # Check if arr[i] is a Fibonacci number
        if isFibonacci(arr[i]):
            current_length += 1
        else:
            current_length = 0
             
        # stores the maximum length of the
        # Fibonacci number subarray
        max_length = max(max_length, current_length)
         
    # Finally, return the maximum length
    return max_length
 
# Driver code
if __name__ == '__main__':
 
    # Given Input
    arr = [ 11, 8, 21, 5, 3, 28, 4 ]
    n = len(arr)
     
    # Function Call
    print(contiguousFibonacciNumber(arr, n))
 
# This code is contributed by MuskanKalra1


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// A utility function that returns
// true if x is perfect square
static bool isPerfectSquare(int x)
{
    int s = (int)Math.Sqrt(x);
    return(s * s == x);
}
 
// Returns true if n is a
// Fibonacci Number, else false
static bool isFibonacci(int n)
{
     
    // Here n is Fibonacci if one of 5*n*n + 4
    // or 5*n*n - 4 or both is a perfect square
    return isPerfectSquare(5 * n * n + 4) ||
           isPerfectSquare(5 * n * n - 4);
}
 
// Function to find the length of the
// largest sub-array of an array every
// element of whose is a Fibonacci number
static int contiguousFibonacciNumber(int []arr, int n)
{
    int current_length = 0;
    int max_length = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Check if arr[i] is a Fibonacci number
        if (isFibonacci(arr[i]))
        {
            current_length++;
        }
        else
        {
            current_length = 0;
        }
 
        // Stores the maximum length of the
        // Fibonacci number subarray
        max_length = Math.Max(max_length,
                              current_length);
    }
 
    // Finally, return the maximum length
    return max_length;
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int []arr = { 11, 8, 21, 5, 3, 28, 4 };
    int n = arr.Length;
 
    // Function Call
    Console.Write(contiguousFibonacciNumber(arr, n));
}
}
         
// This code is contributed by SURENDRA_GANGWAR


Javascript




<script>
      // JavaScript program for the above approach
 
      // A utility function that returns
      // true if x is perfect square
      function isPerfectSquare(x) {
          let s = parseInt(Math.sqrt(x));
          return (s * s == x);
      }
 
      // Returns true if n is a
      // Fibonacci Number, else false
      function isFibonacci(n)
      {
       
          // Here n is Fibonacci if one of 5*n*n + 4
          // or 5*n*n - 4 or both is a perfect square
          return isPerfectSquare(5 * n * n + 4)
              || isPerfectSquare(5 * n * n - 4);
      }
 
      // Function to find the length of the
      // largest sub-array of an array every
      // element of whose is a Fibonacci number
      function contiguousFibonacciNumber(arr, n) {
 
          let current_length = 0;
          let max_length = 0;
 
          // Traverse the array arr[]
          for (let i = 0; i < n; i++) {
 
              // Check if arr[i] is a Fibonacci number
              if (isFibonacci(arr[i])) {
                  current_length++;
              }
              else {
                  current_length = 0;
              }
 
              // Stores the maximum length of the
              // Fibonacci number subarray
              max_length = Math.max(max_length, current_length);
          }
 
          // Finally, return the maximum length
          return max_length;
      }
 
      // Driver code
 
      // Given Input
      let arr = [11, 8, 21, 5, 3, 28, 4];
      let n = arr.length;
 
      // Function Call
      document.write(contiguousFibonacciNumber(arr, n));
 
// This code is contributed by Potta Lokesh
  </script>


Output

4

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Last Updated : 14 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads