
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.
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.
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.
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
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.
Approach: The basic idea behind this approach is to check values from 1 to the maximum element as we know that the GCD of any number of the array can not be bigger than the maximum element. We will also use the property that GCD of multiples of a number is the number itself.
For example:
The GCD of 5 and 10 is 5, similarly if we add another multiple of 5 in the list. i.e GCD of 5, 10, and 15 is still 5, and it remains 5 till we are adding multiples of 5 to the list. This is true for any value and its multiples.
So, in short, we will traverse through all possible GCD options, check for its multiples, and increment if we find the current calculated GCD equal to the GCD options.