Given an array with positive number the task is that we find largest subset from array that contain elements which are Fibonacci numbers.
Asked in Facebook
Examples :
Input : arr[] = {1, 4, 3, 9, 10, 13, 7};
Output : subset[] = {1, 3, 13}
The output three numbers are Fibonacci
numbers.
Input : arr[] = {0, 2, 8, 5, 2, 1, 4,
13, 23};
Output : subset[] = {0, 2, 8, 5, 2, 1,
13}
A simple solution is to iterate through all elements of given array. For every number, check if it is Fibonacci or not. If yes, add it to the result.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void findFibSubset( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
int fact1 = 5 * pow (arr[i], 2) + 4;
int fact2 = 5 * pow (arr[i], 2) - 4;
if (( int ) pow (( int ) pow (fact1, 0.5), 2) == fact1
|| ( int ) pow (( int ) pow (fact2, 0.5), 2) == fact2)
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 4, 2, 8, 5, 20, 1, 40, 13, 23 };
int n = 9;
findFibSubset(arr, n);
}
|
Java
import java.util.*;
class GFG {
static void findFibSubset( int arr[], int n)
{
for ( int i = 0 ; i < n; i++){
int fact1 = 5 * ( int )Math.pow(arr[i], 2 ) + 4 ;
int fact2 = 5 * ( int )Math.pow(arr[i], 2 ) - 4 ;
if (( int )Math.pow(( int )Math.pow(fact1, 0.5 ), 2 ) == fact1 || ( int )Math.pow(( int )Math.pow(fact2, 0.5 ), 2 ) == fact2)
System.out.print(arr[i] + " " );
}
}
public static void main (String[] args) {
int []arr = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 };
int n = arr.length;
findFibSubset(arr, n);
}
}
|
Python3
def findFibSubset(arr, n):
for i in range (n):
fact1 = 5 * (arr[i] * * 2 ) + 4
fact2 = 5 * (arr[i] * * 2 ) - 4
if int (fact1 * * (. 5 )) * * 2 = = fact1 or int (fact2 * * (. 5 )) * * 2 = = fact2:
print (arr[i],end = " " )
return None
if __name__ = = "__main__" :
arr = [ 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 ]
n = len (arr)
findFibSubset(arr, n)
|
C#
using System;
class GFG {
static void findFibSubset( int [] arr, int n)
{
for ( int i = 0; i < n; i++) {
int fact1 = 5 * ( int )Math.Pow(arr[i], 2) + 4;
int fact2 = 5 * ( int )Math.Pow(arr[i], 2) - 4;
if (( int )Math.Pow(( int )Math.Pow(fact1, 0.5), 2)
== fact1
|| ( int )Math.Pow(( int )Math.Pow(fact2, 0.5),
2)
== fact2)
Console.Write(arr[i] + " " );
}
}
static void Main()
{
int [] arr = { 4, 2, 8, 5, 20, 1, 40, 13, 23 };
int n = 9;
findFibSubset(arr, n);
}
}
|
Javascript
function findFibSubset( arr, n)
{
let ans=[];
for (let i = 0; i < n; i++) {
let fact1 = 5 * Math.pow(arr[i], 2) + 4;
let fact2 = 5 * Math.pow(arr[i], 2) - 4;
if (Math.pow(Math.round(Math.pow(fact1, 0.5)), 2) == fact1 || Math.pow(Math.round(Math.pow(fact2, 0.5)), 2) == fact2)
ans.push(arr[i]);
}
console.log(ans);
}
let arr = [ 4, 2, 8, 5, 20, 1, 40, 13, 23 ];
let n = 9;
findFibSubset(arr, n);
|
Time complexity: Time complexity of the above solution is O(n) and space complexity is O(1).
Below is an another solution based on hashing.
- Find max in the array
- Generate Fibonacci numbers till the max and store it in hash table.
- Traverse array again if the number is present in hash table then add it to the result.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void findFibSubset( int arr[], int n)
{
int max = *std::max_element(arr, arr+n);
int a = 0, b = 1;
unordered_set< int > hash;
hash.insert(a);
hash.insert(b);
while (b < max)
{
int c = a + b;
a = b;
b = c;
hash.insert(b);
}
for ( int i=0; i<n; i++)
if (hash.find(arr[i]) != hash.end())
printf ( "%d " , arr[i]);
}
int main()
{
int arr[] = {4, 2, 8, 5, 20, 1, 40, 13, 23};
int n = sizeof (arr)/ sizeof (arr[0]);
findFibSubset(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void findFibSubset(Integer[] x)
{
Integer max = Collections.max(Arrays.asList(x));
List<Integer> fib = new ArrayList<Integer>();
List<Integer> result = new ArrayList<Integer>();
Integer a = 0 ;
Integer b = 1 ;
while (b < max){
Integer c = a + b;
a=b;
b=c;
fib.add(c);
}
for (Integer i = 0 ; i < x.length; i++){
if (fib.contains(x[i])){
result.add(x[i]);
}
}
System.out.println(result);
}
public static void main(String args[])
{
Integer[] a = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 };
findFibSubset(a);
}
}
|
Python3
def findFibSubset(arr, n):
m = max (arr)
a = 0
b = 1
hash = []
hash .append(a)
hash .append(b)
while (b < m):
c = a + b
a = b
b = c
hash .append(b)
for i in range (n):
if arr[i] in hash :
print ( arr[i],end = " " )
if __name__ = = "__main__" :
arr = [ 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 ]
n = len (arr)
findFibSubset(arr, n)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
public static void findFibSubset( int [] x)
{
int max = x.Max();
List< int > fib = new List< int >();
List< int > result = new List< int >();
int a = 0;
int b = 1;
while (b < max)
{
int c = a + b;
a = b;
b = c;
fib.Add(c);
}
for ( int i = 0; i < x.Length; i++)
{
if (fib.Contains(x[i]))
{
result.Add(x[i]);
}
}
foreach ( int i in result)
Console.Write(i + " " );
}
public static void Main(String []args)
{
int [] a = {4, 2, 8, 5, 20, 1, 40, 13, 23};
findFibSubset(a);
}
}
|
Javascript
<script>
function findFibSubset(arr, n)
{
var max = arr.reduce((a,b)=>Math.max(a,b))
var a = 0, b = 1;
var hash = new Set();
hash.add(a);
hash.add(b);
while (b < max)
{
var c = a + b;
a = b;
b = c;
hash.add(b);
}
for ( var i=0; i<n; i++)
if (hash.has(arr[i]))
document.write( arr[i]
+ " " );
}
var arr = [4, 2, 8, 5, 20, 1, 40, 13, 23];
var n = arr.length;
findFibSubset(arr, n);
</script>
|
Time Complexity: Time complexity of above code is O(n) and space complexity will also be O(n) as we are storing it in hash map each fibonacci number in hashmap….
This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...