GCD of Different SubSequences in Java

An array inArr of the positive numbers is given. The task is to find the number of the various GCD (Greatest Common Divisor) in all of the subsequences that are present in the input array that is unique. Note subsequence of an array is made by taking any one or more than one element at a time.

Example 1:

Input

int inArr[] = {6, 8, 10}

Output: 4

Explanation: The different subsequences of the input array and their GCD are:

For subsequence {6}, the GCD is 6.

For subsequence {8}, the GCD is 8.

For subsequence {10}, the GCD 10.

For subsequence {6, 8}, GCD is 2.

For subsequence {8, 10}, GCD is 2.

For subsequence {6, 10}, GCD is 2.

For subsequence {6, 8, 10}, GCD is 2.

Thus, all the computed GCDs are 6, 8, 10, 2, 2, 2, 2, out of which only 4 are unique (6, 8, 10, 2). Hence, the output is 4.

Example 2:

Input

int inArr[] = {2, 4, 8}

Output: 3

Explanation: The different subsequences of the input array and their GCD are:

For subsequence {2}, the GCD is 2.

For subsequence {4}, the GCD is 4.

For subsequence {8}, the GCD 8.

For subsequence {2, 4}, GCD is 2.

For subsequence {4, 8}, GCD is 4.

For subsequence {2, 8}, GCD is 2.

For subsequence {2, 4, 8}, GCD is 2.

Thus, all the computed GCDs are 2, 4, 8, 2, 4, 2, 2, out of which only 3 are unique (2, 4, 8). Hence, the output is 3.

Approach: Brute Force

In this approach, we will be computing all of the subsequences of the input array and will store those subsequences in an array list. Then, we will compute the GCD of all of the subsequences that are stored in the array list and will put them in a set. As the set only contains unique elements, the size of the set will be our answer. For finding the subsequences, we will be using recursion. Observe the following program.

FileName: DiffSubseqGCD.java

Output:

For the input array: 
6 8 10 
The total number of unique GCDs is: 4

For the input array: 
2 4 8 
The total number of unique GCDs is: 3

Complexity Analysis: Each recursively calls leads to two more recursive calls. Thus, the time complexity of the program is exponential. The space complexity of the program is also exponential.

The above program is not suitable for the larger inputs as the time as well as space complexity of the program is too high. Therefore, we must do some optimization to reduce the time complexity and space complexity.

Efficient Approach:

Using the greedy approach, we can do the optimization. We have to use the fact that the GCD of any two numbers n1 and n2 lies between 1 to a maximum of (n1, n2), and the same concept is valid for any subsequence containing more than two elements. If the maximum element in a subsequence is M, then the GCD will always fall within the range [1, M]. Thus, we can do an iteration from 1 to M, and any number in the given range is a factor of an element of the array, then we can display that element as one of the outcomes of the GCD.

FileName: DiffSubseqGCD1.java

Output:

For the input array: 
6 8 10 
The total number of unique GCDs is: 4

For the input array: 
2 4 8 
The total number of unique GCDs is: 3

Complexity Analysis: The time complexity of the program is O(Max * log(Max)), where Max is the maximum element of the array. The space complexity of the program is O(N), where N is the total number of elements present in the array list.