You are given an array ‘ARR’ of size ‘N’ consisting of distinct elements. Your task is to find the total number of good subsets. A subset is called a good subset when you can rearrange the elements of the subset in such a way that each element divides the next element in order. Two subsets are different from each other if there is an element in one subset, which does not exist in the other.
For example :
Consider ARR = [2, 4, 5, 15], all possible good subsets following the given conditions are (2), (4), (5), (15), (2,4), and (5,15). Hence, the total number of good subsets is 6 in this case.
Input Format :
The first line of the input contains an integer, 'T,’ denoting the number of test cases. The 'T' test cases follow.
The first line of each test case contains a single integer, 'N', denoting the number of elements in the array.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
Output Format :
For each test case, print a single integer - the total number of good subsets.
Print the output of each test case in a separate line.
Constraints :
1 <= T <= 10
1 <= N <= 10^5
1 <= ARR[i] <= 10^5
All elements present in the array ARR are unique.
Time limit: 1 sec
The idea is to use a recursive approach to find all subsets of the array. The recursive idea is very clear that there are only two choices for any element in the array, either to include the ...read more
A simple method is to sort the given array and traverse through the array to find the total number of good subsets with each array element as the rear element of the subset.
Our app...read more
In the previous approach, we are traversing through the array to find the total number of subsets in which we can append the array element, but in this approach, we will be travers...read more
import java.util.Arrays;
public class CountGoodSubsets {
public static void main(String[] args) {
int[] ARR = {2, 3, 4, 6};
int result = countGoodSubsets(ARR);
System.out.println("Total number of good ...read more
Top Cloud Analogy Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Cloud Analogy Software Developer
Reviews
Interviews
Salaries
Users/Month