# 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++ program to find the minimum number ` `// of elements the need to be changed ` `// to get first N numbers of Fibonacci series ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that finds minimum changes required ` `int` `fibonacciArray(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `multiset<` `int` `> s; ` ` ` ` ` `// a and b are first two ` ` ` `// fibonacci numbers ` ` ` `int` `a = 1, b = 1; ` ` ` `int` `c; ` ` ` ` ` `// insert first n fibonacci elements to set ` ` ` `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++) { ` ` ` ` ` `// if fibonacci element is present ` ` ` `// in the array then remove it from set ` ` ` `it = s.find(arr[i]); ` ` ` `if` `(it != s.end()) ` ` ` `s.erase(it); ` ` ` `} ` ` ` ` ` `// return the remaining number of ` ` ` `// elements in the set ` ` ` `return` `s.size(); ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Minimum array elements to be changed to make it a Lucas Sequence
- Minimum number of operations on an array to make all elements 0
- Minimum number of elements that should be removed to make the array good
- Minimum number of increment-other operations to make all array elements equal.
- Minimum number of 1's to be replaced in a binary array
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
- Minimum array element changes to make its elements 1 to N
- Minimum gcd operations to make all array elements one
- Minimum operation to make all elements equal in array
- Minimum no. of operations required to make all Array Elements Zero
- Make all array elements equal with minimum cost
- Minimum replacements to make elements of a ternary array same
- Minimum delete operations to make all elements of array same
- Minimum Bitwise AND operations to make any two array elements equal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.