Minimum number of elements to be replaced to make the given array a Fibonacci Sequence Last Updated : 09 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // 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; } Java // Java program to find the minimum number // of elements the need to be changed // to get first N numbers of Fibonacci series import java.util.*; class geeks { // Function that finds minimum changes required public static int fibonacciArray(int[] arr, int n) { Set<Integer> s = new HashSet<Integer>(); // a and b are first two // fibonacci numbers int a = 1, b = 1; int c; // insert first n fibonacci elements to set 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 fibonacci element is present // in the array then remove it from set if (s.contains(arr[i])) s.remove(arr[i]); } // return the remaining number of // elements in the set return s.size(); } // Driver Code 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)); } } // This code is contributed by // sanjeev2552 Python3 # Python3 program to find the minimum number # of elements the need to be changed # to get first N numbers of Fibonacci series # Function that finds minimum changes required def fibonacciArray(arr, n): s = set() # a and b are first two # fibonacci numbers a, b = 1, 1 # insert first n fibonacci elements to set 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 fibonacci element is present in # the array then remove it from set if arr[i] in s: s.remove(arr[i]) # return the remaining number # of elements in the set return len(s) # Driver code if __name__ == "__main__": arr = [3, 1, 21, 4, 2, 1, 8, 9] n = len(arr) print(fibonacciArray(arr, n)) # This code is contributed by Rituraj Jain C# // C# program to find the minimum number // of elements the need to be changed // to get first N numbers of Fibonacci series using System; using System.Collections.Generic; public class geeks { // Function that finds minimum changes required public static int fibonacciArray(int[] arr, int n) { HashSet<int> s = new HashSet<int>(); // a and b are first two // fibonacci numbers int a = 1, b = 1; int c; // insert first n fibonacci elements to set 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 fibonacci element is present // in the array then remove it from set if (s.Contains(arr[i])) s.Remove(arr[i]); } // return the remaining number of // elements in the set return s.Count; } // Driver Code 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)); } } // This code is contributed by Rajput-Ji JavaScript <script> // Javascript program to find the minimum number // of elements the need to be changed // to get first N numbers of Fibonacci series // Function that finds minimum changes required function fibonacciArray(arr, n) { let s = new Set(); // a and b are first two // fibonacci numbers let a = 1, b = 1; let c; // insert first n fibonacci elements to set 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 fibonacci element is present // in the array then remove it from set if (s.has(arr[i])) s.delete(arr[i]); } // return the remaining number of // elements in the set return s.size; } // Driver Code let arr = [3, 1, 21, 4, 2, 1, 8, 9]; let n = arr.length; document.write(fibonacciArray(arr, n)); // This code is contributed by _saurabh_jaiswal </script> Output2 Comment More infoAdvertise with us Next Article Find the minimum number of elements that should be removed to make an array good S Shashank_Sharma Follow Improve Article Tags : DSA Fibonacci cpp-multiset Practice Tags : Fibonacci Similar Reads Find the minimum number of elements that should be removed to make an array good Given an array of size N and an integer K. The array consists of only digits {0, 1, 2, 3, ...k-1}. The task is to make array good by removing some of the elements. The array of length x is called good if x is divisible by k and one can split the given array into x/k subsequences and each of form {0, 6 min read Minimum number of elements that should be removed to make the array good Given an array arr[], the task is to find the minimum number of elements that must be removed to make the array good. A sequence a1, a2 ... an is called good if for each element ai, there exists an element aj (i not equals to j) such that ai + aj is a power of two i.e. 2d for some non-negative integ 13 min read Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M Given an array arr[] of size N consisting of only the first M natural numbers, the task is to find the minimum length of the subarray that is required to be replaced such that the frequency of array elements is N / M. Note: N is a multiple of M. Examples: Input: M = 3, arr[] = {1, 1, 1, 1, 2, 3}Outp 10 min read Minimum number of 1's to be replaced in a binary array Given a binary array arr[] of zero's and one's only. The task is to find the minimum number of one's to be changed to zero such if there exist any index i in the array such that arr[i] = 0 then arr[i-1] and arr[i+1] both should not be equals to 1 at the same time. That is, for any index i the below 5 min read Minimum number of changes required to make the given array an AP Given an array arr[] of N integers and a number d . You can change any element of the array to an integer. The task is to find the minimum number of change(s) required to make the given array an Arithmetic Progression with the common difference d . Examples: Input : N = 4, d = 2 arr[] = {1, 2, 4, 6} 8 min read Minimum replacements to make elements of a ternary array same Given a ternary array (every element has one the three possible values 1, 2 and 3). Our task is to replace the minimum number of numbers in it so that all the numbers in the array are equal to each other. Examples: Input : arr[] = 1 3 2 2 2 1 1 2 3 Output : 5 In this example, frequency of 1 is 3, fr 5 min read Like