Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
Given an array arr containing N integer elements, the task is to count the minimum number of elements that need to be changed such that all the elements (after proper rearrangement) make first N terms of Fibonacci Series.
Examples:
Input: arr[] = {4, 1, 2, 1, 3, 7}
Output: 2
4 and 7 must be changed to 5 and 8 to make first N(6) terms of Fibonacci series.
Input: arr[] = {5, 3, 1, 1, 2, 8, 11}
Output: 1
11 must be changed to 13.
Approach:
- Insert first N elements of Fibonacci series into a multi set.
- Then, traverse the array from left to right and check if the current element is present in multi set.
- If element is present in the multi set then remove it.
- Final answer will be the size of final multi set.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fibonacciArray( int arr[], int n)
{
multiset< int > s;
int a = 1, b = 1;
int c;
s.insert(a);
if (n >= 2)
s.insert(b);
for ( int i = 0; i < n - 2; i++) {
c = a + b;
s.insert(c);
a = b;
b = c;
}
multiset< int >::iterator it;
for ( int i = 0; i < n; i++) {
it = s.find(arr[i]);
if (it != s.end())
s.erase(it);
}
return s.size();
}
int main()
{
int arr[] = { 3, 1, 21, 4, 2, 1, 8, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << fibonacciArray(arr, n);
return 0;
}
|
Java
import java.util.*;
class geeks
{
public static int fibonacciArray( int [] arr, int n)
{
Set<Integer> s = new HashSet<Integer>();
int a = 1 , b = 1 ;
int c;
s.add(a);
if (n > 2 )
s.add(b);
for ( int i = 0 ; i < n - 2 ; i++)
{
c = a + b;
s.add(c);
a = b;
b = c;
}
for ( int i = 0 ; i < n; i++)
{
if (s.contains(arr[i]))
s.remove(arr[i]);
}
return s.size();
}
public static void main(String[] args)
{
int [] arr = { 3 , 1 , 21 , 4 , 2 , 1 , 8 , 9 };
int n = arr.length;
System.out.print(fibonacciArray(arr, n));
}
}
|
Python3
def fibonacciArray(arr, n):
s = set ()
a, b = 1 , 1
s.add(a)
if n > = 2 :
s.add(b)
for i in range ( 0 , n - 2 ):
c = a + b
s.add(c)
a, b = b, c
for i in range ( 0 , n):
if arr[i] in s:
s.remove(arr[i])
return len (s)
if __name__ = = "__main__" :
arr = [ 3 , 1 , 21 , 4 , 2 , 1 , 8 , 9 ]
n = len (arr)
print (fibonacciArray(arr, n))
|
C#
using System;
using System.Collections.Generic;
public class geeks
{
public static int fibonacciArray( int [] arr, int n)
{
HashSet< int > s = new HashSet< int >();
int a = 1, b = 1;
int c;
s.Add(a);
if (n > 2)
s.Add(b);
for ( int i = 0; i < n - 2; i++)
{
c = a + b;
s.Add(c);
a = b;
b = c;
}
for ( int i = 0; i < n; i++)
{
if (s.Contains(arr[i]))
s.Remove(arr[i]);
}
return s.Count;
}
public static void Main(String[] args)
{
int [] arr = { 3, 1, 21, 4, 2, 1, 8, 9 };
int n = arr.Length;
Console.WriteLine(fibonacciArray(arr, n));
}
}
|
Javascript
<script>
function fibonacciArray(arr, n) {
let s = new Set();
let a = 1, b = 1;
let c;
s.add(a);
if (n > 2)
s.add(b);
for (let i = 0; i < n - 2; i++) {
c = a + b;
s.add(c);
a = b;
b = c;
}
for (let i = 0; i < n; i++) {
if (s.has(arr[i]))
s. delete (arr[i]);
}
return s.size;
}
let arr = [3, 1, 21, 4, 2, 1, 8, 9];
let n = arr.length;
document.write(fibonacciArray(arr, n));
</script>
|
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...