Implement the subsetsCountDistinct method that counts distinct subsets that can be formed from the values.

The input array nums contains size distinct values.

Your task is to return the number of different subsets that can be formed from the array. A subset may contain zero, one, many, or all elements.

For example, [1,2,3] has 8 subsets because each of the three elements can either be selected or skipped.

Example 1

Input
nums (int[]) = [1,2,3]
size (int) = 3
Returns (int) 8

Example 2

Input
nums (int[]) = []
size (int) = 0
Returns (int) 1

Example 3

Input
nums (int[]) = [1,2]
size (int) = 2
Returns (int) 4

Each element has two choices: include it in the subset or leave it out.

Since the choices are independent for every element, the total number of distinct subsets is 2^size. The empty subset is included in this count.

Calculate the power of two using a simple loop and return it.

Pseudocode:

function subsetsCountDistinct(nums, size):
    count = 1
    for i from 1 to size:
        count = count * 2
    return count
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.