Length of longest Fibonacci subarray
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++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
int contiguousFibonacciNumber( int arr[], int n)
{
int current_length = 0;
int max_length = 0;
for ( int i = 0; i < n; i++) {
if (isFibonacci(arr[i])) {
current_length++;
}
else {
current_length = 0;
}
max_length = max(max_length, current_length);
}
return max_length;
}
int main()
{
int arr[] = { 11, 8, 21, 5, 3, 28, 4};
int n = sizeof (arr) / sizeof (arr[0]);
cout << contiguousFibonacciNumber(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static boolean isPerfectSquare( int x)
{
int s =( int ) Math.sqrt(x);
return (s * s == x);
}
public static boolean isFibonacci( int n)
{
return isPerfectSquare( 5 * n * n + 4 )
|| isPerfectSquare( 5 * n * n - 4 );
}
public static int contiguousFibonacciNumber( int arr[], int n)
{
int current_length = 0 ;
int max_length = 0 ;
for ( int i = 0 ; i < n; i++) {
if (isFibonacci(arr[i])) {
current_length++;
}
else {
current_length = 0 ;
}
max_length = Math.max(max_length, current_length);
}
return max_length;
}
public static void main (String[] args)
{
int arr[] = { 11 , 8 , 21 , 5 , 3 , 28 , 4 };
int n = arr.length;
System.out.println( contiguousFibonacciNumber(arr, n));
}
}
|
Python3
import math
def isPerfectSquare(x):
s = int (math.sqrt(x))
if s * s = = x:
return True
else :
return False
def isFibonacci(n):
return (isPerfectSquare( 5 * n * n + 4 ) or
isPerfectSquare( 5 * n * n - 4 ))
def contiguousFibonacciNumber(arr, n):
current_length = 0
max_length = 0
for i in range ( 0 , n):
if isFibonacci(arr[i]):
current_length + = 1
else :
current_length = 0
max_length = max (max_length, current_length)
return max_length
if __name__ = = '__main__' :
arr = [ 11 , 8 , 21 , 5 , 3 , 28 , 4 ]
n = len (arr)
print (contiguousFibonacciNumber(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isPerfectSquare( int x)
{
int s = ( int )Math.Sqrt(x);
return (s * s == x);
}
static bool isFibonacci( int n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
static int contiguousFibonacciNumber( int []arr, int n)
{
int current_length = 0;
int max_length = 0;
for ( int i = 0; i < n; i++)
{
if (isFibonacci(arr[i]))
{
current_length++;
}
else
{
current_length = 0;
}
max_length = Math.Max(max_length,
current_length);
}
return max_length;
}
public static void Main()
{
int []arr = { 11, 8, 21, 5, 3, 28, 4 };
int n = arr.Length;
Console.Write(contiguousFibonacciNumber(arr, n));
}
}
|
Javascript
<script>
function isPerfectSquare(x) {
let s = parseInt(Math.sqrt(x));
return (s * s == x);
}
function isFibonacci(n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
function contiguousFibonacciNumber(arr, n) {
let current_length = 0;
let max_length = 0;
for (let i = 0; i < n; i++) {
if (isFibonacci(arr[i])) {
current_length++;
}
else {
current_length = 0;
}
max_length = Math.max(max_length, current_length);
}
return max_length;
}
let arr = [11, 8, 21, 5, 3, 28, 4];
let n = arr.length;
document.write(contiguousFibonacciNumber(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...