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 product of bitonic 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)
- Maximum length Subsequence with alternating sign and maximum Sum
- Find a sorted subsequence of size 3 in linear time
- Sorted subsequence of size 3 in linear time using constant space
- Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
- 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)
- Subsequence with maximum odd sum
- Maximum even sum subsequence
- Maximum Sum Subsequence of length k