Count Good Subsets

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
CodingNinjas
author
2y
Recursion

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

CodingNinjas
author
2y
Dynamic Programming

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

CodingNinjas
author
2y
Sieve based approach

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

Puja Tiwari
1y

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

Add answer anonymously...
Cloud Analogy Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter