Longest sub-sequence of array containing Lucas numbers
Given an array arr[] of N elements, the task is to find the length of the longest sub-sequence in arr[] such that all the elements of the sequence are Lucas Numbers.
Examples:
Input: arr[] = {2, 3, 55, 6, 1, 18}
Output: 4
1, 2, 3 and 18 are the only elements from the Lucas sequence.
Input: arr[] = {22, 33, 2, 123}
Output: 2
Approach:
- Find the maximum element in the array.
- Generate Lucas numbers upto to the max and store them in a set.
- Traverse the array arr[] and check if the current element is present in the set.
- If it is present in the set, and increment the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LucasSequence( int arr[], int n)
{
int max = *max_element(arr, arr+n);
unordered_set< int > s;
int a = 2, b = 1, c;
s.insert(a);
s.insert(b);
while (b < max) {
int c = a + b;
a = b;
b = c;
s.insert(b);
}
int count = 0;
for ( int i = 0; i < n; i++) {
auto it = s.find(arr[i]);
if (it != s.end())
count++;
}
return count;
}
int main()
{
int arr[] = { 7, 11, 22, 4, 2, 1, 8, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << LucasSequence(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int LucasSequence( int [] arr, int n)
{
int max = Arrays.stream(arr).max().getAsInt();
int counter = 0 ;
HashSet<Integer> s = new HashSet<>();
int a = 2 , b = 1 ;
s.add(a);
s.add(b);
while (b < max)
{
int c = a + b;
a = b;
b = c;
s.add(b);
}
for ( int i = 0 ; i < n; i++)
{
if (s.contains(arr[i]))
{
counter++;
}
}
return counter;
}
public static void main(String[] args)
{
int [] arr = { 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 };
int n = arr.length;
System.out.println(LucasSequence(arr, n));
}
}
|
Python3
def LucasSequence(arr, n):
max = arr[ 0 ]
for i in range ( len (arr)):
if (arr[i] > max ):
max = arr[i]
s = set ()
a = 2
b = 1
s.add(a)
s.add(b)
while (b < max ):
c = a + b
a = b
b = c
s.add(b)
count = 0
for i in range (n):
if (arr[i] in s):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 ]
n = len (arr)
print (LucasSequence(arr, n))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int LucasSequence( int []arr, int n)
{
int max = arr.Max();
int counter = 0;
HashSet< int > s = new HashSet< int >() ;
int a = 2, b = 1 ;
s.Add(a);
s.Add(b);
while (b < max)
{
int c = a + b;
a = b;
b = c;
s.Add(b);
}
for ( int i = 0; i < n; i++)
{
if (s.Contains(arr[i]))
counter++;
}
return counter;
}
static public void Main()
{
int []arr = { 7, 11, 22, 4, 2, 1, 8, 9 };
int n = arr.Length ;
Console.WriteLine(LucasSequence(arr, n)) ;
}
}
|
Javascript
<script>
function LucasSequence(arr, n)
{
var max = arr.reduce((a,b)=> Math.max(a,b));
var s = [];
var a = 2, b = 1, c;
s.push(a);
s.push(b);
while (b < max) {
var c = a + b;
a = b;
b = c;
s.push(b);
}
s.sort((a,b) => a-b)
var count = 0;
for ( var i = 0; i < n; i++) {
if (s.includes(arr[i]))
{
s.pop(arr[i]);
count++;
}
}
return count;
}
var arr = [7, 11, 22, 4, 2, 1, 8, 9 ];
var n = arr.length;
document.write( LucasSequence(arr, n));
</script>
|
Time complexity: O(n), where n is the number of elements in the input array. This is because the function iterates through the array once and performs a constant-time lookup for each element in the unordered_set.
Auxiliary Space: O(n), where n is the number of elements in the input array. This is because the function creates an unordered_set that stores all the Lucas numbers up to the maximum element in the input array. The size of this set is determined by the number of Lucas numbers that are less than or equal to the maximum element in the input array, which is directly proportional to the number of elements in the input array.
Last Updated :
31 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...