We are given an array of positive integers and an integer k. Find the maximum possible GCD of a sub sequence of size k.
Input : arr = [2, 1, 4, 6] k = 3 Output : 2 GCD of [2, 4, 6] is 2 Input : arr = [1, 2, 3] k = 3 Output : 1 GCD of [1, 2, 3] is 1
Method 1 Generate all the sub sequences of size k one by one and then find the GCD of all such generated sub sequences. Print the largest found GCD.
Method 2 In this method, we maintain a count array to store the count of divisors of every element. We will traverse the given array and for every element we will calculate its divisors and increment at the index of count array. The process of computing divisors will take O(sqrt(arr[i])) time, where arr[i] is element in the given array at index i. After the whole traversal, we can simply traverse the count array from last index to index 1. If we find an index with value equal to or greater than k, then this means that it is a divisor of at least k elements and also the max GCD.
- Maximum product of subsequence of size k
- Maximum product of an increasing subsequence of size 3
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Longest Increasing Subsequence Size (N log N)
- Find a sorted subsequence of size 3 in linear time
- Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
- Sorted subsequence of size 3 in linear time using constant space
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum even sum subsequence
- Subsequence with maximum odd sum
- Maximum Sum Decreasing Subsequence
- Maximum Sum Increasing Subsequence | DP-14
- Maximum Sum Subsequence of length k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.