You are given an array “ARR” of positive integers. Your task is to find the number of different GCD’s in all the non-empty subsequences of the given array.
Note:
The GCD of a list of numbers is defined as the greatest number that perfectly divides all the members of the given list. For example, the GCD of 6, 8, and 10 is 2.
A subsequence of the array is a list that can be made by using some elements of the given array. All the elements can also be part of a subsequence.
For example: the array [1, 2] has subsequences: [1], [2] and [1, 2]. Similarly for the array [1, 2, 3], one of the subsequences is [1, 3].
The first line contains an integer ‘T’, which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains an integer ‘N’ denoting the size of the array.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
Output Format:
For each test case, print the count of different GCD’s in all the non-empty subsequences of the given array.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
1 <= ARR[i] <= 10^5
Where ‘T’ is the number of test cases, ‘N’, denotes the size of the array 'ARR', and ‘ARR[i]’ denotes the elements of the array.
Time limit: 1 sec
2
3
6 8 10
3
1 2 1
4
2
In the first test case,
The subsequences and their GCD’s are:
The different GCD’s from the above table are 6, 8, 10, 2. So, the answer is 4.

In the second test case,
The subsequences and their GCD’s are:
The different GCD’s from the above table are 1, 2. So, the answer is 2.
.png)
2
4
5 10 15 20
3
2 5 10
4
4
In the first test case,
The different GCD’s are 5, 10, 15, 20. So, the answer is 4.
In the second test case,
The different GCD’s are 1, 2, 5, 10. So, the answer is 4.
Can you think of creating all the subsequences?
Approach: The simple idea behind this approach is to find all possible values of gcd from the given array, store them in a hashMap and finally count the elements stored in the hashMap, which will be equal to the count of different GCD subsequences.
Algorithm:
O(2 ^ N), where N is the size of the array.
Since we are recursively calling 2 times for every index, and the recursion depth is O(N) in the worst case. Hence, the overall Time Complexity is O(2 ^ N).
O(N), where N is the size of the array.
Since we are using arrays to store GCD values, and the array will contain N elements, and also using the recursion stack, the overall Space Complexity is O(N).