# How many relations are possible in set A such that n A )= 2?

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 = 2Output: 4The given set A = {1, 2}. The following

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:CPP

// C++ Program to count reflexive relations// on a set of first n natural numbers.#include <iostream>using namespace std; int countReflexive(int n) {// Return 2^(n*n - n) return (1 << (n*n - n)); }int main() { int n = 3; cout << countReflexive(n); return 0; }    Java

// Java Program to count reflexive// relations on a set of first n// natural numbers.import java.io.*; import java.util.*; class GFG { static int countReflexive(int n) {// Return 2^(n*n - n) return (1 << (n*n - n)); }// Driver functionpublic static void main (String[] args) { int n = 3; System.out.println(countReflexive(n)); } }// This code is contributed by Gitanjali.    Python3

# Python3 Program to count# reflexive relations# on a set of first n# natural numbers.def countReflexive(n): # Return 2^(n*n - n) return (1 << (n*n - n)); # driver functionn = 3 ans = countReflexive(n); print (ans) # This code is contributed by saloni1297    C#

// C# Program to count reflexive// relations on a set of first n// natural numbers.using System; class GFG { static int countReflexive(int n) { // Return 2^(n*n - n) return (1 << (n*n - n)); } // Driver function public static void Main () { int n = 3; Console.WriteLine(countReflexive(n)); } }// This code is contributed by vt_m.    PHP

<?php// PHP Program to count // reflexive relations on a // set of first n natural numbers.function countReflexive(\$n) {// Return 2^(n * n - n) return (1 << (\$n * \$n - \$n)); }//Driver code\$n = 3; echo countReflexive(\$n); // This code is contributed by mits ?>    Javascript

<script>// Javascript Program to count reflexive // relations on a set of first n // natural numbers. function countReflexive(n) { // Return 2^(n*n - n) return (1 << (n*n - n)); } let n = 3; document.write(countReflexive(n)); // This code is contributed by divyesh072019. </script>   Output64

Time Complexity: O(1)
Auxiliary Space: O(1)                                                                                                                                    Recommended

Solve DSA problems on GfG Practice.                                                                                                                                        Solve Problems
Article Tags :                    Bit MagicCombinatorialMathematical                          Recommended Articles             1. Number of relations that are neither Reflexive nor Irreflexive on a Set            2. Reflexive Relation on Set            3. Number of Irreflexive Relations on a Set            4. Number of Antisymmetric Relations on a set of N elements            5. Number of Asymmetric Relations on a set of N elements            6. Number of Symmetric Relations on  a Set            7. Number of Relations that are both Irreflexive and Antisymmetric on a Set            8. Prime Number of Set Bits in Binary Representation | Set 2            9. Prime Number of Set Bits in Binary Representation | Set 1            10. Program to find the Nth natural number with exactly two bits set | Set 2            11. Check whether the number has only first and last bits set | Set 2            12. Next higher number with same number of set bits            13. Minimum number using set bits of a given number            14. Count number of triplets with product equal to given number with duplicates allowed | Set-2            15. Calculate the number of set bits for every number from 0 to N            16. Smallest number dividing minimum number of elements in the array | Set 2            17. Minimum number of squares whose sum equals to given number N | set 2            18. Minimum number of squares whose sum equals to a given number N  | Set-3            19. Highest power of a number that divides other number | Set - 2            20. Number of integers with odd number of set bits            21. Find the largest number smaller than integer N with maximum number of set bits            22. Count number of subsets of a set with GCD equal to a given number            23. M-th smallest number having k number of set bits.            24. Check if a number has same number of set and unset bits            25. Number of factors of very large number N modulo M where M is any prime number                       Read Full Article