Count all ordered pairs (A, B) such that A and B are disjoint
Last Updated :
29 Feb, 2024
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:
- Set the modulo constant MOD to 1e9 + 7.
- Define a function binpow() that calculates the power using binary exponentiation.
- Take the modulo of the base to keep it within the specified range.
- Initialize result to 1.
- Iterate until the exponent is greater than 0.
- If the current bit of the exponent is 1, multiply the result by the base.
- Square the base and divide the exponent by 2 in each iteration.
- Return the final result.
- Solve Function:
- Define a function solve that takes an integer n and a vector arr.
- Call binpow(3, n) to calculate (3^n) mod (1e9 + 7).
- Return the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
long long binpow( long long base, long long exp )
{
base %= MOD;
long long result = 1;
while ( exp
> 0) {
if ( exp & 1)
result = (result * base)
% MOD;
base = (base * base) % MOD;
exp >>= 1;
}
return result;
}
int solve( int n, vector< int >& arr)
{
return binpow(3, n);
}
int main()
{
int n = 3;
vector< int > arr = { 1, 2, 3 };
cout << solve(n, arr);
return 0;
}
|
Java
import java.util.*;
public class Main {
static final int MOD = 1000000007 ;
static long binpow( long base, long exp) {
base %= MOD;
long result = 1 ;
while (exp > 0 ) {
if ((exp & 1 ) == 1 )
result = (result * base) % MOD;
base = (base * base) % MOD;
exp >>= 1 ;
}
return result;
}
static int solve( int n, List<Integer> arr) {
return ( int ) binpow( 3 , n);
}
public static void main(String[] args) {
int n = 3 ;
List<Integer> arr = Arrays.asList( 1 , 2 , 3 );
System.out.println(solve(n, arr));
}
}
|
C#
using System;
using System.Collections.Generic;
public class Program
{
const int MOD = 1000000007;
static long Binpow( long baseVal, long exp)
{
baseVal %= MOD;
long result = 1;
while (exp > 0)
{
if ((exp & 1) == 1)
result = (result * baseVal) % MOD;
baseVal = (baseVal * baseVal) % MOD;
exp >>= 1;
}
return result;
}
static int Solve( int n, List< int > arr)
{
return ( int )Binpow(3, n);
}
public static void Main( string [] args)
{
int n = 3;
List< int > arr = new List< int > { 1, 2, 3 };
Console.WriteLine(Solve(n, arr));
}
}
|
Javascript
const MOD = BigInt(1e9 + 7);
function binpow(base, exp) {
base %= MOD;
let result = BigInt(1);
while (exp > 0n) {
if (exp & 1n)
result = (result * base) % MOD;
base = (base * base) % MOD;
exp >>= 1n;
}
return result;
}
function solve(n, arr) {
return binpow(3n, BigInt(n));
}
const n = 3;
const arr = [1, 2, 3];
console.log(solve(n, arr).toString());
|
Python3
def binpow(base, exp, mod):
base % = mod
result = 1
while exp > 0 :
if exp & 1 :
result = (result * base) % mod
base = (base * base) % mod
exp >> = 1
return result
def solve(n, arr):
MOD = 10 * * 9 + 7
return binpow( 3 , n, MOD)
if __name__ = = "__main__" :
n = 3
arr = [ 1 , 2 , 3 ]
print (solve(n, arr))
|
Time Complexity: O(log(n))
Auxiliary Space: O(log(n))
Share your thoughts in the comments
Please Login to comment...