Length of longest Fibonacci subarray formed by removing only one element
Last Updated :
04 Jan, 2023
Given an array A containing integers, the task is to find the length of longest Fibonacci subarray formed by removing only one element from the array.
Examples:
Input: arr[] = { 2, 8, 5, 7, 3, 5, 7 }
Output: 5
Explanation:
If we remove the number 7 at index 3, then the remaining array contains a Fibonacci subarray {2, 8, 5, 3, 5} of length 5, which is maximum.
Input: arr[] = { 2, 3, 6, 1 }
Output: 3
Explanation:
If we remove the number 6 at index 2, then the remaining array contains a Fibonacci subarray {2, 3, 1} of length 3, which is maximum.
Approach: The above-mentioned problem can be solved by counting the contiguous Fibonacci numbers just before every index and just after every index.
- Now traverse the array again and find an index for which counts of Fibonacci numbers after and before is maximum.
- In order to check for Fibonacci numbers, we will build a hash table containing all the Fibonacci numbers less than or equal to the maximum value in the array to test a number in O(1) time.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
#define N 100000
void createHash(set< int >& hash,
int maxElement)
{
int prev = 0, curr = 1;
hash.insert(prev);
hash.insert(curr);
while (curr <= maxElement) {
int temp = curr + prev;
hash.insert(temp);
prev = curr;
curr = temp;
}
}
int longestFibSubarray(
int arr[], int n)
{
int max_val
= *max_element(arr, arr + n);
set< int > hash;
createHash(hash, max_val);
int left[n], right[n];
int fibcount = 0, res = -1;
for ( int i = 0; i < n; i++) {
left[i] = fibcount;
if (hash.find(arr[i])
!= hash.end()) {
fibcount++;
}
else
fibcount = 0;
}
fibcount = 0;
for ( int i = n - 1; i >= 0; i--) {
right[i] = fibcount;
if (hash.find(arr[i])
!= hash.end()) {
fibcount++;
}
else
fibcount = 0;
}
for ( int i = 0; i < n; i++)
res = max(
res,
left[i] + right[i]);
return res;
}
int main()
{
int arr[] = { 2, 8, 5, 7, 3, 5, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << longestFibSubarray(arr, n)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 100000 ;
static void createHash(HashSet<Integer> hash,
int maxElement)
{
int prev = 0 , curr = 1 ;
hash.add(prev);
hash.add(curr);
while (curr <= maxElement) {
int temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
static int longestFibSubarray(
int arr[], int n)
{
int max_val = Arrays.stream(arr).max().getAsInt();
HashSet<Integer> hash = new HashSet<Integer>();
createHash(hash, max_val);
int []left = new int [n];
int []right = new int [n];
int fibcount = 0 , res = - 1 ;
for ( int i = 0 ; i < n; i++) {
left[i] = fibcount;
if (hash.contains(arr[i])) {
fibcount++;
}
else
fibcount = 0 ;
}
fibcount = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
right[i] = fibcount;
if (hash.contains(arr[i])) {
fibcount++;
}
else
fibcount = 0 ;
}
for ( int i = 0 ; i < n; i++)
res = Math.max(
res,
left[i] + right[i]);
return res;
}
public static void main(String[] args)
{
int arr[] = { 2 , 8 , 5 , 7 , 3 , 5 , 7 };
int n = arr.length;
System.out.print(longestFibSubarray(arr, n)
+ "\n" );
}
}
|
Python3
N = 100000
def createHash( hash , maxElement) :
prev = 0
curr = 1
hash .add(prev)
hash .add(curr)
while (curr < = maxElement) :
temp = curr + prev
hash .add(temp)
prev = curr
curr = temp
def longestFibSubarray(arr, n) :
max_val = max (arr)
hash = { int }
createHash( hash , max_val)
left = [ 0 for i in range (n)]
right = [ 0 for i in range (n)]
fibcount = 0
res = - 1
for i in range (n) :
left[i] = fibcount
if (arr[i] in hash ) :
fibcount + = 1
else :
fibcount = 0
fibcount = 0
for i in range (n - 1 , - 1 , - 1 ) :
right[i] = fibcount
if (arr[i] in hash ) :
fibcount + = 1
else :
fibcount = 0
for i in range ( 0 ,n) :
res = max (res, left[i] + right[i])
return res
arr = [ 2 , 8 , 5 , 7 , 3 , 5 , 7 ]
n = len (arr)
print (longestFibSubarray(arr, n))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG{
static readonly int N = 100000;
static void createHash(HashSet< int > hash,
int maxElement)
{
int prev = 0, curr = 1;
hash.Add(prev);
hash.Add(curr);
while (curr <= maxElement) {
int temp = curr + prev;
hash.Add(temp);
prev = curr;
curr = temp;
}
}
static int longestFibSubarray(
int []arr, int n)
{
int max_val = arr.Max();
HashSet< int > hash = new HashSet< int >();
createHash(hash, max_val);
int []left = new int [n];
int []right = new int [n];
int fibcount = 0, res = -1;
for ( int i = 0; i < n; i++) {
left[i] = fibcount;
if (hash.Contains(arr[i])) {
fibcount++;
}
else
fibcount = 0;
}
fibcount = 0;
for ( int i = n - 1; i >= 0; i--) {
right[i] = fibcount;
if (hash.Contains(arr[i])) {
fibcount++;
}
else
fibcount = 0;
}
for ( int i = 0; i < n; i++)
res = Math.Max(
res,
left[i] + right[i]);
return res;
}
public static void Main(String[] args)
{
int []arr = { 2, 8, 5, 7, 3, 5, 7 };
int n = arr.Length;
Console.Write(longestFibSubarray(arr, n)
+ "\n" );
}
}
|
Javascript
<script>
let N = 100000;
function createHash( hash, maxElement)
{
let prev = 0, curr = 1;
hash.add(prev);
hash.add(curr);
while (curr <= maxElement) {
let temp = curr + prev;
hash.add(temp);
prev = curr;
curr = temp;
}
}
function longestFibSubarray(arr, n)
{
let max_val = Math.max(...arr);
let hash = new Set();
createHash(hash, max_val);
let left = Array.from({length: n}, (_, i) => 0);
let right = Array.from({length: n}, (_, i) => 0);
let fibcount = 0, res = -1;
for (let i = 0; i < n; i++) {
left[i] = fibcount;
if (hash.has(arr[i])) {
fibcount++;
}
else
fibcount = 0;
}
fibcount = 0;
for (let i = n - 1; i >= 0; i--) {
right[i] = fibcount;
if (hash.has(arr[i])) {
fibcount++;
}
else
fibcount = 0;
}
for (let i = 0; i < n; i++)
res = Math.max(
res,
left[i] + right[i]);
return res;
}
let arr = [ 2, 8, 5, 7, 3, 5, 7 ];
let n = arr.length;
document.write(longestFibSubarray(arr, n)
+ "<br/>" );
</script>
|
Time Complexity: O(n + log(m)), where n is the size of the given array and m is the maximum element in the array.
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...