Minimum array elements to be changed to make it a Lucas Sequence
Last Updated :
12 Sep, 2022
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. Lucas terms may be present in any order in the array.
Examples:
Input: arr[] = {29, 1, 3, 4, 5, 11, 18, 2}
Output: 1
Explanation: 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
Explanation: 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++
#include <bits/stdc++.h>
using namespace std;
int lucasArray( int arr[], int N)
{
set< int > s;
int a = 2, b = 1;
int c;
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++) {
it = s.find(arr[i]);
if (it != s.end())
s.erase(it);
}
return s.size();
}
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
import java.util.HashSet;
import java.util.Set;
class GfG {
static int lucasArray( int arr[], int n)
{
HashSet<Integer> s = new HashSet<>();
int a = 2 , b = 1 , c;
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 (s.contains(arr[i]))
s.remove(arr[i]);
}
return s.size();
}
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));
}
}
|
Python3
def lucasArray(arr, n):
s = set ()
a = 2
b = 1
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 (arr[i] in s):
s.remove(arr[i])
return len (s)
if __name__ = = '__main__' :
arr = [ 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 ]
n = len (arr)
print (lucasArray(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int lucasArray( int [] arr, int n)
{
HashSet< int > s = new HashSet< int >();
int a = 2, b = 1, c;
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 (s.Contains(arr[i]))
s.Remove(arr[i]);
}
return s.Count;
}
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));
}
}
|
Javascript
<script>
function lucasArray(arr, n)
{
var s = new Set();
var a = 2, b = 1;
var c;
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++) {
s. delete (arr[i])
}
return s.size;
}
var arr = [7, 11, 22, 4, 2, 1, 8, 9 ];
var n = arr.length;
document.write( lucasArray(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N * log(N))
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...