![]() |
VOOZH | about |
Given a number n, find out the number of Reflexive Relation on a set of first n natural numbers {1, 2, ..n}.
Examples :
Input: n = 2
Output: 4
The given set A = {1, 2}. The following are reflexive relations on A * A :
{{1, 1), (2, 2)}
{(1, 1), (2, 2), (1, 2)}
{(1, 1), (2, 2), (1, 2), (2, 1)}
{(1, 1), (2, 2), (2, 1)}Input: n = 3
Output: 64
Explanation :
Reflexive Relation: A Relation R on A a set A is said to be Reflexive if xRx for every element of x ? A.
The number of reflexive relations on an n-element set is 2n(n-1)
How does this formula work?
A relation R is reflexive if the matrix diagonal elements are 1.
If we take a closer look the matrix, we can notice that the size of matrix is n2. The n diagonal entries are fixed. For remaining n2 - n entries, we have choice to either fill 0 or 1. So there are total 2n(n-1) ways of filling the matrix.
Below is the code implementation of the above approach:
64
Time Complexity: O(1)
Auxiliary Space: O(1)