Smallest subsequence having GCD equal to GCD of given array
Last Updated :
09 Mar, 2021
Given an array arr[] of size N, the task is to find the smallest subsequence of the given array whose GCD of the subsequence is equal to the GCD of the given array. If more than one such subsequence exists, then print anyone of them.
Examples:
Input: arr[] = {4, 6, 12}
Output: 4 6
Explanation: Smallest subsequence having gcd equal to gcd of the given array(= 2) is {4, 6}. Therefore, the required output is {4, 6}
Input: arr[] = {6, 12, 18, 24}
Output: 6
Naive Approach: The simplest approach to solve this problem is to generate all possible subsequences of the given array and calculate GCD of each subsequence. Print the smallest subsequence having gcd equal to gcd of the given array.
Time Complexity: O(2N * N * log X), where X is the maximum element of the given array.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is based on the following observations:
Length of the smallest subsequence having gcd equal to gcd of the given array must be either 1 or 2.
Consider, gcd of the given array is Y.
If arr[idx] = Y, then length of the smallest subsequence must be 1 for some value of idx.
Otherwise, length of the smallest subsequence must be 2.
Proof using Contradiction method:
If gcd of all possible pairs of the given array is greater than Y, then gcd of the given array must be greater than Y, which is not possible.
Therefore, at least one pair exists in the given array whose gcd is equal to Y.
Follow the steps below to solve the problem:
- Initialize a variable gcdArr to store GCD of the array.
- Traverse the given array and check if any array element is equal to gcdArr or not. If found to be true, print that element.
- Otherwise, print a pair from the given array whose gcd is equal to gcdArr.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void printSmallSub( int arr[], int N)
{
int gcdArr = 0;
for ( int i = 0; i < N; i++) {
gcdArr = __gcd(gcdArr, arr[i]);
}
for ( int i = 0; i < N; i++) {
if (arr[i] == gcdArr) {
cout << arr[i] << " " ;
return ;
}
}
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N;
j++) {
if (__gcd(arr[i], arr[j])
== gcdArr) {
cout << arr[i] << " " << arr[j];
return ;
}
}
}
}
int main()
{
int arr[] = { 4, 6, 12 };
int N = sizeof (arr) / sizeof (arr[0]);
printSmallSub(arr, N);
}
|
Java
import java.io.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static void printSmallSub( int [] arr, int N)
{
int gcdArr = 0 ;
for ( int i = 0 ; i < N; i++)
{
gcdArr = gcd(gcdArr, arr[i]);
}
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == gcdArr)
{
System.out.print(arr[i] + " " );
return ;
}
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
if (gcd(arr[i], arr[j]) == gcdArr)
{
System.out.print(arr[i] + " " +
arr[j]);
return ;
}
}
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 12 };
int N = arr.length;
printSmallSub(arr, N);
}
}
|
Python3
import math
def printSmallSub(arr, N):
gcdArr = 0
for i in range ( 0 , N):
gcdArr = math.gcd(gcdArr, arr[i])
for i in range ( 0 , N):
if (arr[i] = = gcdArr):
print (arr[i], end = " " )
return
for i in range ( 0 , N):
for j in range (i + 1 , N):
if (math.gcd(arr[i],
arr[j]) = = gcdArr):
print (arr[i], end = " " )
print (arr[j], end = " " )
return
if __name__ = = "__main__" :
arr = [ 4 , 6 , 12 ]
N = len (arr)
printSmallSub(arr, N)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void printSmallSub( int [] arr, int N)
{
int gcdArr = 0;
for ( int i = 0; i < N; i++)
{
gcdArr = gcd(gcdArr, arr[i]);
}
for ( int i = 0; i < N; i++)
{
if (arr[i] == gcdArr)
{
Console.Write(arr[i] + " " );
return ;
}
}
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
if (gcd(arr[i], arr[j]) == gcdArr)
{
Console.Write(arr[i] + " " +
arr[j]);
return ;
}
}
}
}
public static void Main()
{
int [] arr = { 4, 6, 12 };
int N = arr.Length;
printSmallSub(arr, N);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function printSmallSub(arr, N)
{
let gcdArr = 0;
for (let i = 0; i < N; i++) {
gcdArr = gcd(gcdArr, arr[i]);
}
for (let i = 0; i < N; i++) {
if (arr[i] == gcdArr) {
document.write(arr[i] + " " );
return ;
}
}
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N;
j++) {
if (gcd(arr[i], arr[j])
== gcdArr) {
document.write(arr[i] + " " + arr[j]);
return ;
}
}
}
}
let arr = [ 4, 6, 12 ];
let N = arr.length;
printSmallSub(arr, N);
</script>
|
Time Complexity: (N2 * log X), where X is the maximum element of the given array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...