📌  相关文章
📜  更改为使其成为Lucas序列的最小数组元素

📅  最后修改于: 2021-05-31 18:26:30             🧑  作者: Mango

给定一个具有N个不同元素的数组。任务是找到要在数组中更改的元素的最小数量,以使该数组包含前N个Lucas序列
注意:Lucas术语可以以任何顺序出现在数组中。
例子

方法:

  • 在集合中插入第一个N(输入数组的大小)Lucas序列项。
  • 从左到右遍历数组,并检查数组元素是否存在于集合中。
  • 如果存在,请将其从集中移除。
  • 所需的最小更改是最终剩余集的大小。

下面是上述方法的实现:

C++
// C++ program to find the minimum number
// of elements to be changed in the array
// to make it a Lucas Sequence
#include 
using namespace std;
 
// Function that finds minimum changes to
// be made in the array
int lucasArray(int arr[], int n)
{
    set s;
 
    // a and b are first two
    // lucas numbers
    int a = 2, b = 1;
    int c;
 
    // insert first n lucas elements to set
    s.insert(a);
    if (n >= 2)
        s.insert(b);
 
    for (int i = 0; i < n - 2; i++) {
        s.insert(a + b);
        c = a + b;
        a = b;
        b = c;
    }
 
    set::iterator it;
    for (int i = 0; i < n; i++) {
        // if lucas element is present in array,
        // 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[] = { 7, 11, 22, 4, 2, 1, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << lucasArray(arr, n);
 
    return 0;
}


Java
// Java program to find the minimum number
// of elements to be changed in the array
// to make it a Lucas Sequence
import java.util.HashSet;
import java.util.Set;
 
class GfG
{
 
    // Function that finds minimum changes
    // to be made in the array
    static int lucasArray(int arr[], int n)
    {
        HashSet s = new HashSet<>();
     
        // a and b are first two lucas numbers
        int a = 2, b = 1, c;
     
        // insert first n lucas elements to set
        s.add(a);
        if (n >= 2)
            s.add(b);
     
        for (int i = 0; i < n - 2; i++)
        {
            s.add(a + b);
            c = a + b;
            a = b;
            b = c;
        }
     
        for (int i = 0; i < n; i++)
        {
            // if lucas element is present in array,
            // 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[] = { 7, 11, 22, 4, 2, 1, 8, 9 };
        int n = arr.length;
     
        System.out.println(lucasArray(arr, n));
    }
}
     
// This code is contributed by Rituraj Jain


Python3
# Python 3 program to find the minimum number
# of elements to be changed in the array
# to make it a Lucas Sequence
 
# Function that finds minimum changes to
# be made in the array
def lucasArray(arr, n):
    s = set()
 
    # a and b are first two
    # lucas numbers
    a = 2
    b = 1
 
    # insert first n lucas elements to set
    s.add(a)
    if (n >= 2):
        s.add(b)
 
    for i in range(n - 2):
        s.add(a + b)
        c = a + b
        a = b
        b = c
 
    for i in range(n):
         
        # if lucas element is present in array,
        # 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 = [7, 11, 22, 4, 2, 1, 8, 9]
    n = len(arr)
 
    print(lucasArray(arr, n))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find the minimum number
// of elements to be changed in the array
// to make it a Lucas Sequence
using System;
using System.Collections.Generic;
     
class GFG
{
 
    // Function that finds minimum changes
    // to be made in the array
    static int lucasArray(int []arr, int n)
    {
        HashSet s = new HashSet();
     
        // a and b are first two lucas numbers
        int a = 2, b = 1, c;
     
        // insert first n lucas elements to set
        s.Add(a);
        if (n >= 2)
            s.Add(b);
     
        for (int i = 0; i < n - 2; i++)
        {
            s.Add(a + b);
            c = a + b;
            a = b;
            b = c;
        }
     
        for (int i = 0; i < n; i++)
        {
            // if lucas element is present in array,
            // 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 = { 7, 11, 22, 4, 2, 1, 8, 9 };
        int n = arr.Length;
     
        Console.WriteLine(lucasArray(arr, n));
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
3
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”