Remove all the fibonacci numbers from the given array
Given an array arr[] of N integers, the task is to remove all the fibonacci numbers present in the array.
Examples:
Input: arr[] = {4, 6, 5, 3, 8, 7, 10, 11, 14, 15}
Output: 4 6 7 10 11 14 15
Explanation:
The array contains 3 fibonacci data values 5, 3 and 8.
These values have been removed from the array.
Input: arr[] = {2, 4, 7, 8, 9, 11}
Output: 4 7 9 11
Explanation:
The array contains 2 fibonacci data values 2 and 8.
These values have been removed from the array.
Approach: The idea is to use hashing to precompute and store the Fibonacci numbers, and then check if a node contains a Fibonacci value in O(1) time.
- Traverse the array and check if the current number is a Fibonacci or not using the precomputed hash.
- If it is, then left shift all the elements after it to remove this Fibonacci number and decrease the value of the array length.
- Repeat the above steps for all the elements of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e3;
set< int > fib;
void fibonacci()
{
int prev = 0, curr = 1, len = 2;
fib.insert(prev);
fib.insert(curr);
while (len <= sz) {
int temp = curr + prev;
fib.insert(temp);
prev = curr;
curr = temp;
len++;
}
}
void printArray( int arr[], int len)
{
for ( int i = 0; i < len; i++) {
cout << arr[i] << ' ' ;
}
}
void removeFibonacci( int arr[], int len)
{
fibonacci();
for ( int i = 0; i < len; i++) {
if (fib.find(arr[i]) != fib.end()) {
for ( int j = i; j < len; j++) {
arr[j] = arr[j + 1];
}
i--;
len--;
}
}
printArray(arr, len);
}
int main()
{
int arr[] = { 4, 6, 5, 3, 8, 7,
10, 11, 14, 15 };
int len = sizeof (arr) / sizeof ( int );
removeFibonacci(arr, len);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int sz = ( int ) 1e3;
static HashSet<Integer> fib = new HashSet<Integer>();
static void fibonacci()
{
int prev = 0 , curr = 1 , len = 2 ;
fib.add(prev);
fib.add(curr);
while (len <= sz) {
int temp = curr + prev;
fib.add(temp);
prev = curr;
curr = temp;
len++;
}
}
static void printArray( int arr[], int len)
{
for ( int i = 0 ; i < len; i++) {
System.out.print(arr[i] + " " );
}
}
static void removeFibonacci( int arr[], int len)
{
fibonacci();
for ( int i = 0 ; i < len; i++) {
if (fib.contains(arr[i])) {
for ( int j = i; j < len - 1 ; j++) {
arr[j] = arr[j + 1 ];
}
i--;
len--;
}
}
printArray(arr, len);
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 5 , 3 , 8 , 7 ,
10 , 11 , 14 , 15 };
int len = arr.length;
removeFibonacci(arr, len);
}
}
|
Python3
sz = 1000
fib = set ()
def fibonacci():
prev , curr , length = 0 , 1 , 2
fib.add(prev)
fib.add(curr)
while (length < = sz):
temp = curr + prev
fib.add(temp)
prev = curr
curr = temp
length + = 1
def printArray( arr, length):
for i in range (length):
print (arr[i],end = " " )
def removeFibonacci( arr, length):
fibonacci()
for i in fib:
if i in arr:
arr.remove(i)
length - = 1
printArray(arr, length)
if __name__ = = "__main__" :
arr = [ 4 , 6 , 5 , 3 , 8 , 7 ,
10 , 11 , 14 , 15 ]
length = len (arr)
removeFibonacci(arr, length)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int sz = ( int ) 1e3;
static HashSet< int > fib = new HashSet< int >();
static void fibonacci()
{
int prev = 0, curr = 1, len = 2;
fib.Add(prev);
fib.Add(curr);
while (len <= sz) {
int temp = curr + prev;
fib.Add(temp);
prev = curr;
curr = temp;
len++;
}
}
static void printArray( int []arr, int len)
{
for ( int i = 0; i < len; i++) {
Console.Write(arr[i] + " " );
}
}
static void removeFibonacci( int []arr, int len)
{
fibonacci();
for ( int i = 0; i < len; i++) {
if (fib.Contains(arr[i])) {
for ( int j = i; j < len - 1; j++) {
arr[j] = arr[j + 1];
}
i--;
len--;
}
}
printArray(arr, len);
}
public static void Main(String[] args)
{
int []arr = { 4, 6, 5, 3, 8, 7,
10, 11, 14, 15 };
int len = arr.Length;
removeFibonacci(arr, len);
}
}
|
Javascript
<script>
let sz = 1e3;
let fib = new Set();
function fibonacci()
{
let prev = 0, curr = 1, len = 2;
fib.add(prev);
fib.add(curr);
while (len <= sz) {
let temp = curr + prev;
fib.add(temp);
prev = curr;
curr = temp;
len++;
}
}
function printArray(arr, len)
{
for (let i = 0; i < len; i++) {
document.write(arr[i] + " " );
}
}
function removeFibonacci(arr, len)
{
fibonacci();
for (let i = 0; i < len; i++) {
if (fib.has(arr[i])) {
for (let j = i; j < len - 1; j++) {
arr[j] = arr[j + 1];
}
i--;
len--;
}
}
printArray(arr, len);
}
let arr = [ 4, 6, 5, 3, 8, 7,
10, 11, 14, 15 ];
let len = arr.length;
removeFibonacci(arr, len);
</script>
|
Output:
4 6 7 10 11 14 15
Time Complexity: O(n2)
Auxiliary Space: O(n), where n is the size of the given array.
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...