Subsequence of size k with maximum possible GCD
We are given an array of positive integers and an integer k. Find the maximum possible GCD of a subsequence of size k.
Examples:
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 subsequences of size k one by one and then find the GCD of all such generated subsequences. 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 the 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 the last index to index 1. If we find an index with a 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.
Steps to solve this problem:
1. declare a variable and store maximum element of the array in it.
2. declare an array divisors of size high+1 and initialize it with 0.
3. iterate through i=0 till n:
*iterate through j=1 till sqrt(arr[i]):
*check if arr[i]%j is equal to 0 than divisors[j]++ and divisors[arr[i]/j]++ if j is not equal to arr[i]/j.
4. iterate through i=high till i>=1:
*check if divisors[i]>=k than return i.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxGCD( int arr[], int n, int k)
{
int high = *max_element(arr, arr+n);
int divisors[high + 1] = { 0 };
for ( int i = 0; i < n; i++) {
for ( int j = 1; j <= sqrt (arr[i]); j++) {
if (arr[i] % j == 0) {
divisors[j]++;
if (j != arr[i] / j)
divisors[arr[i] / j]++;
}
}
}
for ( int i = high; i >= 1; i--)
if (divisors[i] >= k)
return i;
}
int main()
{
int arr[] = { 1, 2, 4, 8, 8, 12 };
int k = 3;
int n = sizeof (arr) / sizeof (arr[0]);
cout << findMaxGCD(arr, n, k);
return 0;
}
|
Java
import java .io.*;
import java .util.*;
class GFG
{
static int findMaxGCD( int []arr,
int n, int k)
{
Arrays.sort(arr);
int high = arr[n - 1 ];
int []divisors = new int [high + 1 ];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 1 ;
j <= Math.sqrt(arr[i]);
j++)
{
if (arr[i] % j == 0 )
{
divisors[j]++;
if (j != arr[i] / j)
divisors[arr[i] / j]++;
}
}
}
for ( int i = high; i >= 1 ; i--)
if (divisors[i] >= k)
return i;
return 0 ;
}
static public void main (String[] args)
{
int []arr = { 1 , 2 , 4 ,
8 , 8 , 12 };
int k = 3 ;
int n = arr.length;
System.out.println(findMaxGCD(arr, n, k));
}
}
|
Python 3
import math
def findMaxGCD(arr, n, k):
high = max (arr)
divisors = [ 0 ] * (high + 1 )
for i in range (n) :
for j in range ( 1 , int (math.sqrt(arr[i])) + 1 ):
if (arr[i] % j = = 0 ) :
divisors[j] + = 1
if (j ! = arr[i] / / j):
divisors[arr[i] / / j] + = 1
for i in range (high, 0 , - 1 ):
if (divisors[i] > = k):
return i
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 8 , 8 , 12 ]
k = 3
n = len (arr)
print (findMaxGCD(arr, n, k))
|
C#
using System;
using System.Linq;
public class GFG {
static int findMaxGCD( int []arr, int n, int k)
{
int high = arr.Max();
int []divisors = new int [high+1];
for ( int i = 0; i < n; i++) {
for ( int j = 1; j <= Math.Sqrt(arr[i]); j++)
{
if (arr[i] % j == 0) {
divisors[j]++;
if (j != arr[i] / j)
divisors[arr[i] / j]++;
}
}
}
for ( int i = high; i >= 1; i--)
if (divisors[i] >= k)
return i;
return 0 ;
}
static public void Main ()
{
int []arr = { 1, 2, 4, 8, 8, 12 };
int k = 3;
int n = arr.Length;
Console.WriteLine(findMaxGCD(arr, n, k));
}
}
|
Javascript
<script>
function findMaxGCD(arr,n,k)
{
arr.sort( function (a,b){ return a-b;});
let high = arr[n - 1];
let divisors = new Array(high + 1);
for (let i=0;i<divisors.length;i++)
{
divisors[i]=0;
}
for (let i = 0; i < n; i++)
{
for (let j = 1;
j <= Math.sqrt(arr[i]);
j++)
{
if (arr[i] % j == 0)
{
divisors[j]++;
if (j != Math.floor(arr[i] / j))
divisors[(Math.floor(arr[i] / j))]++;
}
}
}
for (let i = high; i >= 1; i--)
if (divisors[i] >= k)
return i;
return 0 ;
}
let arr=[1, 2, 4,
8, 8, 12];
let k = 3;
let n = arr.length;
document.write(findMaxGCD(arr, n, k));
</script>
|
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...