📜  使给定数组成为斐波那契数列的最少元素数

📅  最后修改于: 2021-06-25 18:51:43             🧑  作者: Mango

给定一个包含N个整数元素的数组arr ,任务是计算需要更改的最小元素数,以使所有元素(在适当重新排列后)成为Fibonacci级数的前N个项。

例子:

方法:

  • 将斐波那契数列的前N个元素插入到多集合中。
  • 然后,从左到右遍历该数组,并检查当前元素是否存在于多集合中。
  • 如果元素在多组中存在,则将其删除。
  • 最终答案将是最终多集的大小。

下面是上述方法的实现:

C++
// C++ program to find the minimum number
// of elements the need to be changed
// to get first N numbers of Fibonacci series
#include 
using namespace std;
  
// Function that finds minimum changes required
int fibonacciArray(int arr[], int n)
{
    multiset 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::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 s = new HashSet();
  
        // 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 s = new HashSet();
  
        // 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


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。