Given an array arr[] of size N, the task is to find the composite Fibonacci numbers present in the given array.
Examples:
Input: arr[] = {13, 55, 7, 3, 5, 21, 233, 144, 6}
Output: 55 21 144
Explanation:
Composite array elements are {55, 21, 144, 6}.
Fibonacci array elements are {55, 21, 144}.
Therefore, array elements which are both composite as well as Fibonacci are {55, 21, 144}.
Input: arr[] = {34, 13, 11, 8, 3, 55, 233}
Output: 3
Explanation:
Composite array elements are {34, 8, 55}
Fibonacci array elements are {34, 8, 55}
Therefore, array elements which are both composite as well as Fibonacci are {34, 8, 55}.
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
set<int> createhashmap(int Max)
{
set<int> hashmap;
int curr = 1;
int prev = 0;
hashmap.insert(prev);
while (curr <= Max) {
hashmap.insert(curr);
int temp = curr;
curr = curr + prev;
prev = temp;
}
return hashmap;
}
vector<bool> SieveOfEratosthenes(
int Max)
{
vector<bool> isPrime(Max, true);
isPrime[0] = false;
isPrime[1] = false;
for (int p = 2; p * p <= Max; p++) {
if (isPrime[p]) {
for (int i = p * p; i <= Max;
i += p) {
isPrime[i] = false;
}
}
}
return isPrime;
}
int cntFibonacciPrime(int arr[], int N)
{
int Max = arr[0];
for (int i = 1; i < N; i++) {
Max = max(Max, arr[i]);
}
vector<bool> isPrime
= SieveOfEratosthenes(Max);
set<int> hashmap
= createhashmap(Max);
for (int i = 0; i < N; i++) {
if (arr[i] == 1)
continue;
if ((hashmap.count(arr[i]))
&& !isPrime[arr[i]]) {
cout << arr[i] << " ";
}
}
}
int main()
{
int arr[] = { 13, 55, 7, 3, 5, 21,
233, 144, 89 };
int N = sizeof(arr) / sizeof(arr[0]);
cntFibonacciPrime(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean[] isPrime;
static HashSet<Integer>
createhashmap(int Max)
{
HashSet<Integer> hashmap =
new HashSet<>();
int curr = 1;
int prev = 0;
hashmap.add(prev);
while (curr < Max)
{
hashmap.add(curr);
int temp = curr;
curr = curr + prev;
prev = temp;
}
return hashmap;
}
static void SieveOfEratosthenes(int Max)
{
isPrime = new boolean[Max];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int p = 2;
p * p <= Max; p++)
{
if (isPrime[p])
{
for (int i = p * p; i <= Max;
i += p)
{
isPrime[i] = false;
}
}
}
}
static void cntFibonacciPrime(int arr[],
int N)
{
int Max = arr[0];
for (int i = 1; i < N; i++)
{
Max = Math.max(Max, arr[i]);
}
SieveOfEratosthenes(Max);
HashSet<Integer> hashmap =
createhashmap(Max);
for (int i = 0; i < N; i++)
{
if (arr[i] == 1)
continue;
if ((hashmap.contains(arr[i])) &&
!isPrime[arr[i]])
{
System.out.print(arr[i] + " ");
}
}
}
public static void main(String[] args)
{
int arr[] = {13, 55, 7, 3, 5,
21, 233, 144, 89};
int N = arr.length;
cntFibonacciPrime(arr, N);
}
}
|
Python3
import math
def createhashmap(Max):
hashmap = {""}
curr = 1
prev = 0
hashmap.add(prev)
while (curr <= Max):
hashmap.add(curr)
temp = curr
curr = curr + prev
prev = temp
return hashmap
def SieveOfEratosthenes(Max):
isPrime = [1 for x in range(Max + 1)]
isPrime[0] = 0
isPrime[1] = 0
for p in range(0, int(math.sqrt(Max))):
if (isPrime[p]):
for i in range(2 * p, Max, p):
isPrime[i] = 0
return isPrime
def cntFibonacciPrime(arr, N):
Max = arr[0]
for i in range(0, N):
Max = max(Max, arr[i])
isPrime = SieveOfEratosthenes(Max)
hashmap = createhashmap(Max)
for i in range(0, N):
if arr[i] == 1:
continue
if ((arr[i] in hashmap) and
(not(isPrime[arr[i]]))):
print(arr[i], end = " ")
arr = [ 13, 55, 7, 3, 5,
21, 233, 144, 89 ]
N = len(arr)
cntFibonacciPrime(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool[] isPrime;
static HashSet<int> createhashmap(int Max)
{
HashSet<int> hashmap = new HashSet<int>();
int curr = 1;
int prev = 0;
hashmap.Add(prev);
while (curr < Max)
{
hashmap.Add(curr);
int temp = curr;
curr = curr + prev;
prev = temp;
}
return hashmap;
}
static void SieveOfEratosthenes(int Max)
{
isPrime = new bool[Max];
for(int i = 0;i<Max;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int p = 2; p * p <= Max; p++)
{
if (isPrime[p])
{
for(int i = p * p; i <= Max;
i += p)
{
isPrime[i] = false;
}
}
}
}
static void cntFibonacciPrime(int []arr,
int N)
{
int Max = arr[0];
for(int i = 1; i < N; i++)
{
Max = Math.Max(Max, arr[i]);
}
SieveOfEratosthenes(Max);
HashSet<int> hashmap = createhashmap(Max);
for(int i = 0; i < N; i++)
{
if (arr[i] == 1)
continue;
if ((hashmap.Contains(arr[i])) &&
!isPrime[arr[i]])
{
Console.Write(arr[i] + " ");
}
}
}
public static void Main(String[] args)
{
int []arr = { 13, 55, 7, 3, 5,
21, 233, 144, 89 };
int N = arr.Length;
cntFibonacciPrime(arr, N);
}
}
|
Javascript
<script>
function createhashmap(Max)
{
var hashmap = new Set();
var curr = 1;
var prev = 0;
hashmap.add(prev);
while (curr <= Max)
{
hashmap.add(curr);
var temp = curr;
curr = curr + prev;
prev = temp;
}
return hashmap;
}
function SieveOfEratosthenes(Max)
{
var isPrime = Array(Max + 1).fill(true);
isPrime[0] = false;
isPrime[1] = false;
for(var p = 2; p * p <= Max; p++)
{
if (isPrime[p])
{
for(var i = p * p; i <= Max;
i += p)
{
isPrime[i] = false;
}
}
}
return isPrime;
}
function cntFibonacciPrime(arr, N)
{
var Max = arr[0];
for(var i = 1; i < N; i++)
{
Max = Math.max(Max, arr[i]);
}
var isPrime = SieveOfEratosthenes(Max);
var hashmap = createhashmap(Max);
for(var i = 0; i < N; i++)
{
if (arr[i] == 1)
continue;
if (hashmap.has(arr[i]) &&
!isPrime[arr[i]])
{
document.write( arr[i] + " ");
}
}
}
var arr = [ 13, 55, 7, 3, 5, 21,
233, 144, 89 ];
var N = arr.length;
cntFibonacciPrime(arr, N);
</script>
|
Time Complexity: O(N + Max * log(log(Max))), where Max is the largest element in the array
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...