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++ 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 ` ` ` `// elemnets 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Minimum gcd operations to make all array elements one
- Minimum array element changes to make its elements 1 to N
- Minimum operation to make all elements equal in array
- Minimum delete operations to make all elements of array same
- Make all array elements equal with minimum cost
- Minimum replacements to make elements of a ternary array same
- Minimum no. of operations required to make all Array Elements Zero
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum number of increment-other operations to make all array elements equal.
- Minimum number of moves to make all elements equal
- Make two sets disjoint by removing minimum elements
- Minimum increment by k operations to make all elements equal
- Minimum operations of given type to make all elements of a matrix equal
- Minimum operations to make XOR of array zero

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.