Skip to content
Related Articles

Related Articles

Improve Article

Minimum array elements to be changed to make it a Lucas Sequence

  • Difficulty Level : Easy
  • Last Updated : 11 May, 2021

Given an array with N distinct elements. The task is to find the minimum number of elements to be changed in the array such that, the array contains first N Lucas Sequence terms
Note: Lucas terms may be present in any order in the array.
Examples
 

Input : arr[] = {29, 1, 3, 4, 5, 11, 18, 2} 
Output : 1 
5 must be changed to 7, to get first N(8) terms of Lucas Sequence. 
Hence, 1 change is required
Input : arr[] = {4, 2, 3, 1} 
Output : 0 
All elements are already first N(4) terms in Lucas sequence. 
 

 

Approach: 
 

  • Insert first N(size of input array) Lucas Sequence terms in a set.
  • Traverse array from left to right and check if array element is present in the set.
  • If it is present that remove it from the set.
  • Minimum changes required is the size of the final remaining set.

Below is the implementation of the above approach: 
 

C++




// C++ program to find the minimum number
// of elements to be changed in the array
// to make it a Lucas Sequence
#include <bits/stdc++.h>
using namespace std;
 
// Function that finds minimum changes to
// be made in the array
int lucasArray(int arr[], int n)
{
    set<int> 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<int>::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<Integer> 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<int> s = new HashSet<int>();
     
        // 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




<script>
 
// Javascript 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
function lucasArray(arr, n)
{
    var s = new Set();
 
    // a and b are first two
    // lucas numbers
    var a = 2, b = 1;
    var c;
 
    // insert first n lucas elements to set
    s.add(a);
    if (n >= 2)
        s.add(b);
 
    for (var i = 0; i < n - 2; i++) {
        s.add(a + b);
        c = a + b;
        a = b;
        b = c;
    }
 
    for (var i = 0; i < n; i++) {
        // if lucas element is present in array,
        // remove it from set
     
        s.delete(arr[i])
    }
 
    // return the remaining number of
    // elements in the set
    return s.size;
}
 
// Driver code
var arr = [7, 11, 22, 4, 2, 1, 8, 9 ];
var n = arr.length;
document.write( lucasArray(arr, n));
 
</script>
Output: 
3

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :