Longest sub-sequence of array containing Lucas numbers Last Updated : 31 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the length of // the longest required sub-sequence int LucasSequence(int arr[], int n) { // Find the maximum element from // the array int max = *max_element(arr, arr+n); // Insert all lucas numbers // below max to the set // a and b are first two elements // of the Lucas sequence 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++) { // If current element is a Lucas // number, increment count auto it = s.find(arr[i]); if (it != s.end()) count++; } // Return the count return count; } // Driver code 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 // Java implementation of the approach import java.util.*; class GFG { // Function to return the length of // the longest required sub-sequence static int LucasSequence(int[] arr, int n) { // Find the maximum element from // the array int max = Arrays.stream(arr).max().getAsInt(); int counter = 0; // Insert all lucas numbers // below max to the set // a and b are first two elements // of the Lucas sequence 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 current element is a Lucas // number, increment count if (s.contains(arr[i])) { counter++; } } // Return the count return counter; } // 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(LucasSequence(arr, n)); } } // This code has been contributed by 29AjayKumar Python3 # Python 3 implementation of the approach # Function to return the length of # the longest required sub-sequence def LucasSequence(arr, n): # Find the maximum element from # the array max = arr[0] for i in range(len(arr)): if(arr[i] > max): max = arr[i] # Insert all lucas numbers below max # to the set a and b are first two # elements of the Lucas sequence 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 current element is a Lucas # number, increment count if(arr[i] in s): count += 1 # Return the count return count # Driver code if __name__ == '__main__': arr = [7, 11, 22, 4, 2, 1, 8, 9] n = len(arr) print(LucasSequence(arr, n)) # This code is contributed by # Surendra_Gangwar C# // C# implementation of the approach using System; using System.Collections.Generic; using System.Linq; class GFG { // Function to return the length of // the longest required sub-sequence static int LucasSequence(int []arr, int n) { // Find the maximum element from // the array int max = arr.Max(); int counter = 0; // Insert all lucas numbers // below max to the set // a and b are first two elements // of the Lucas sequence 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 current element is a Lucas // number, increment count if (s.Contains(arr[i])) counter++; } // Return the count return counter; } // Driver code static public void Main() { int []arr = { 7, 11, 22, 4, 2, 1, 8, 9 }; int n = arr.Length ; Console.WriteLine(LucasSequence(arr, n)) ; } } // This code is contributed by Ryuga JavaScript <script> // Javascript implementation of the approach // Function to return the length of // the longest required sub-sequence function LucasSequence(arr, n) { // Find the maximum element from // the array var max = arr.reduce((a,b)=> Math.max(a,b)); // push all lucas numbers // below max to the set // a and b are first two elements // of the Lucas sequence 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 current element is a Lucas // number, increment count if(s.includes(arr[i])) { s.pop(arr[i]); count++; } } // Return the count return count; } // Driver code var arr = [7, 11, 22, 4, 2, 1, 8, 9 ]; var n = arr.length; document.write( LucasSequence(arr, n)); </script> Output: 5 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. Comment More infoAdvertise with us Next Article Longest sub-sequence of array containing Lucas numbers S Shashank_Sharma Follow Improve Article Tags : Misc Mathematical Hash DSA Arrays +1 More Practice Tags : ArraysHashMathematicalMisc Similar Reads Length of longest subsequence of Fibonacci Numbers in an Array Given an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9, 5 min read Number of Longest Increasing Subsequences Given an array arr[] of size n, the task is to count the number of longest increasing subsequences present in the given array.Examples:Input: arr[] = [2, 2, 2, 2, 2]Output: 5Explanation: The length of the longest increasing subsequence is 1, i.e. {2}. Therefore, count of longest increasing subsequen 15+ min read Longest subsequence having equal numbers of 0 and 1 Given a binary array, the task is to find the size of the largest sub_sequence which having equal number of zeros and one. Examples : Input : arr[] = { 1, 0, 0, 1, 0, 0, 0, 1 } Output: 6 Input : arr[] = { 0, 0, 1, 1, 1, 1, 1, 0, 0 }Output : 8 simple solution is that we generate all possible sub_sequ 11 min read Sum of numbers from 1 to N which are in Lucas Sequence Given a number N. The task is to find the sum of numbers from 1 to N, which are present in the Lucas Sequence. The Lucas numbers are in the following integer sequence: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123 ...... Examples: Input : N = 10 Output : 17 Input : N = 5 Output : 10 Approach: Loop through 4 min read Longest alternating subsequence A sequence {X1, X2, .. Xn} is an alternating sequence if its elements satisfy one of the following relations : X1 < X2 > X3 < X4 > X5 < â¦. xn or X1 > X2 < X3 > X4 < X5 > â¦. xn Examples: Input: arr[] = {1, 5, 4}Output: 3Explanation: The whole arrays is of the form x1 15+ min read Longest Increasing Subsequence(LIS) using two arrays Given two arrays A[] and B[] of size N. Your Task is to find Longest Increasing Subsequence(LIS) in another array C[] such that array C[] is formed by following rules: C[i] = A[i] or C[i] = B[i] for all i from 1 to N.Examples: Input: A[] = {2, 3, 1}, B[] = {1, 2, 1}Output: 2Explanation: Form C[] as 9 min read Longest sub-sequence that satisfies the given conditions Given an array arr[] of N integers, the task is to find the longest sub-sequence in the given array such that for all pairs from the sub-sequence (arr[i], arr[j]) where i != j either arr[i] divides arr[j] or vice versa. If no such sub-sequence exists then print -1.Examples: Input: arr[] = {2, 4, 6, 7 min read Find the length of the longest valid number chain in an Array Given an array A[] of N numbers, the task is to find the length of the longest valid number that can be formed by connecting one or more numbers from the array such that, while connecting two numbers the last digit of the previous number is the same as the first digit of the next number. Examples: I 10 min read Longest Increasing consecutive subsequence | Set-2 Given an array arr[] of N elements, the task is to find the length of the longest increasing subsequence whose adjacent element difference is one. Examples: Input: arr[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output: 6 Explanation: The subsequence {3, 4, 5, 6, 7, 8} is the longest increasing subsequenc 5 min read Longest Bitonic Subsequence in O(n log n) Given an array arr[0 ⦠n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as an argument and returns the length of the longest bitonic subsequence. A sequence, sorted in increasing order is co 15+ min read Like