A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Given an array arr[] of N positive integers. The task is to find the length of the shortest sub-sequence such that the GCD of the subsequence is 1. If none of the sub-sequence has GCD 1, then print “-1“.
Examples:
Input:_ arr[] = {2, 6, 12, 3}_
Output:_ 2 _
Explanation:
The GCD of 2, 3 = 1, which is the smallest length of subsequence as 2.
Input:_ arr[] = {2, 4}_
Output:_ -1_
Explanation:
_GCD of 2, 4 = 2 _
Naive Approach: The idea is to generate all possible subsequence of the given array and print the length of that subsequence whose GCD is unity and have a minimum length. If none of the sub-sequence has GCD 1, then print “-1“.
Time Complexity:_ O(2N)_
Auxiliary Space:_ O(1)_
Efficient Approach: There are 2 key observations for solving this problem:
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
// Java program for the above approach
**import**
java.io.*;
**import**
java.util.*;
**class**
GFG {
// Function that finds the prime
// factors of a number
**private**
**static**
**int**``[] findPrimeFactors(``**int**
n)
{
// To store the prime factor
**int**``[] primeFactors =
**new**
**int**``[``9``];
**int**
j =
0``;
// 2s that divide n
**if**
(n %
2
==
0``) {
primeFactors[j++] =
2``;
**while**
(n %
2
==
0``)
n >>=
1``;
}
// N must be odd at this point
// Skip one element
**for**
(``**int**
i =
3``;
i * i <= n; i +=
2``) {
**if**
(n % i ==
0``) {
// Update the prime factor
primeFactors[j++] = i;
**while**
(n % i ==
0``)
n /= i;
}
}
// If n is a prime number
// greater than 2
**if**
(n >
2``)
primeFactors[j++] = n;
**return**
Arrays.copyOfRange(primeFactors,
0``, j);
}
// Function that finds the shortest
// subsequence
**private**
**static**
**void**
findShortestSubsequence(``**int**``[] dp,
**int**``[] a,
**int**
index,
**int**``[] primeFactors)
{
**int**
n = a.length;
**for**
(``**int**
j = index; j < n; j++) {
**int**
bitmask =
0``;
**for**
(``**int**
p =
0``;
p < primeFactors.length; p++) {
// Check if the prime factor
// of first number, is also
// the prime factor of the
// rest numbers in array
**if**
(a[j] % primeFactors[p] ==
0``) {
// Set corresponding bit
// of prime factor to 1,
// it means both these
// numbers have the
// same prime factor
bitmask ^= (``1
<< p);
}
}
**for**
(``**int**
i =
0``;
i < dp.length; i++) {
// If no states encountered
// so far continue for this
// combination of bits
**if**
(dp[i] == n +
1``)
**continue**``;
// Update this state with
// minimum ways to reach
// this state
dp[bitmask & i]
= Math.min(dp[bitmask & i],
dp[i] +
1``);
}
}
}
// Function that print the minimum
// length of subsequence
**private**
**static**
**void**
printMinimumLength(``**int**``[] a)
{
**int**
min = a.length +
1``;
**for**
(``**int**
i =
0``;
i < a.length -
1``; i++) {
// Find the prime factors of
// the first number
**int**``[] primeFactors
= findPrimeFactors(a[i]);
**int**
n = primeFactors.length;
**int**``[] dp =
**new**
**int**``[``1
<< n];
// Initialize the array with
// maximum steps, size of the
// array + 1 for instance
Arrays.fill(dp, a.length +
1``);
// Express the prime factors
// in bit representation
// Total number of set bits is
// equal to the total number
// of prime factors
**int**
setBits = (``1
<< n) -
1``;
// Indicates there is one
// way to reach the number
// under consideration
dp[setBits] =
1``;
findShortestSubsequence(dp, a, i +
1``,
primeFactors);
// State 0 corresponds
// to gcd of 1
min = Math.min(dp[``0``], min);
}
// If not found such subsequence
// then print "-1"
**if**
(min == a.length +
1``)
System.out.println(-``1``);
// Else print the length
**else**
System.out.println(min);
}
// Driver Code
**public**
**static**
**void**
main(String[] args)
{
// Given array arr[]
**int**``[] arr = {
2``,
6``,
12``,
3
};
// Function Call
printMinimumLength(arr);
}
}
Output:
2
Time Complexity:_ O(N2)_
Auxiliary Space:_ O(1)_
Better Approach: The minimum length of a sub-sequence having GCD equals to 1 can’t be more than 2, because if a sub-sequence have GCD equals to 1 then it can be proved that the sub-sequence has at least one co-prime pair. Below are the steps:
arrays bit magic dynamic programming mathematical algorithms-dynamic programming bitwise-and gcd-lcm subsequence
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.