![]() |
VOOZH | about |
Given an array arr[] of size n contains distinct elements, the task is to find the number of ordered pairs (A, B) that can be made where A and B are subsets of the given array arr[] and A ∩ B = Φ (i.e, Both A and B subset should be disjoint or no common elements between them).
Example:
Input: arr = {1, 2}
Output: 9
Explanation: The subsets of the array are {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, and {1, 2, 3}. The ordered pairs (A, B) where A and B disjoint subsets are: ({}, {}), ({}, {1}), ({}, {2}), ({1}, {}), ({2}, {}), ({1, 2}, {}), ({}, {1, 2}, ({1}, {2}, ({2}, {1})Input: arr = {1, 2, 3}
Output: 27
Approach:
Every element has three options, It will either be included in A, B, or not included in either A or B. Hence, there would be 3^n possible combinations for given array of size n.
Steps-by-step approach:
Below is the implementation of the above approach:
27
Time Complexity: O(log(n))
Auxiliary Space: O(log(n))