# Number of Relations that are both Irreflexive and Antisymmetric on a Set

Given a positive integer **N**, the task is to find the number of relations that are irreflexive antisymmetric relations that can be formed over the given set of elements. Since the count can be very large, print it to modulo **10 ^{9} + 7**.

A relation

Ron a setAis called reflexive if no(a, a)€Rholds for every elementa € A.

For Example: If set A = {a, b} then R = {(a, b), (b, a)} is irreflexive relation.A relation

Ron a setAis calledAntisymmetricif and only if(a, b) € Rand(b, a) € R, thena = bis called antisymmetric, i.e., the relationR = {(a, b)→ R | a ≤ b} is anti-symmetric, sincea ≤ bandb ≤ aimpliesa = b.

**Examples:**

Input:N = 2Output:3Explanation:

Considering the set {a, b}, all possible relations that are both irreflexive and antisymmetric relations are:

- {}
- {{a, b}}
- {{b, a}}

Input:N = 5Output:59049

**Approach:** The given problem can be solved based on the following observations:

- A relation
**R**on a set**A**is a subset of the**Cartesian Product**of a set, i.e., A * A with**N**.^{2}elements - No
**(x, x)**pair should be included in the subset to make sure the relation is**irreflexive**. - For the remaining
**(N**pairs, divide them into^{2}– N)**(N**where each group consists of a pair^{2}– N)/2 groups**(x, y)**and its symmetric pair**(y, x)**. - Now, there are
**three choices**, either include one of the ordered pairs or neither of them from a group. - Therefore, the total number of possible relations that are both irreflexive and antisymmetric is given by
**3**.^{(N2 – N)/2}

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <iostream>` `using` `namespace` `std;` `const` `int` `mod = 1000000007;` `// Function to calculate` `// x ^ y % mod in O(log y)` `int` `power(` `long` `long` `x,` `unsigned ` `int` `y)` `{` ` ` `// Stores the result of x^y` ` ` `int` `res = 1;` ` ` `// Update x if it exceeds mod` ` ` `x = x % mod;` ` ` `while` `(y > 0) {` ` ` `// If y is odd, then multiply` ` ` `// x with result` ` ` `if` `(y & 1)` ` ` `res = (res * x) % mod;` ` ` `// Divide y by 2` ` ` `y = y >> 1;` ` ` `// Update the value of x` ` ` `x = (x * x) % mod;` ` ` `}` ` ` `// Return the value of x^y` ` ` `return` `res;` `}` `// Function to count relations that` `// are irreflexive and antisymmetric` `// in a set consisting of N elements` `int` `numberOfRelations(` `int` `N)` `{` ` ` `// Return the resultant count` ` ` `return` `power(3, (N * N - N) / 2);` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 2;` ` ` `cout << numberOfRelations(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `static` `int` `mod = ` `1000000007` `;` `// Function to calculate` `// x ^ y % mod in O(log y)` `static` `int` `power(` `long` `x, ` `int` `y)` `{` ` ` ` ` `// Stores the result of x^y` ` ` `int` `res = ` `1` `;` ` ` `// Update x if it exceeds mod` ` ` `x = x % mod;` ` ` `while` `(y > ` `0` `)` ` ` `{` ` ` `// If y is odd, then multiply` ` ` `// x with result` ` ` `if` `(y % ` `2` `== ` `1` `)` ` ` `res = (` `int` `)(res * x) % mod;` ` ` `// Divide y by 2` ` ` `y = y >> ` `1` `;` ` ` `// Update the value of x` ` ` `x = (x * x) % mod;` ` ` `}` ` ` `// Return the value of x^y` ` ` `return` `res;` `}` `// Function to count relations that` `// are irreflexive and antisymmetric` `// in a set consisting of N elements` `static` `int` `numberOfRelations(` `int` `N)` `{` ` ` ` ` `// Return the resultant count` ` ` `return` `power(` `3` `, (N * N - N) / ` `2` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `2` `;` ` ` `System.out.print(numberOfRelations(N));` `}` `}` `// This code is contributed by 29AjayKumar` |

## Python3

`# Python 3 program for the above approach` `mod ` `=` `1000000007` `# Function to calculate` `# x ^ y % mod in O(log y)` `def` `power(x, y):` ` ` `# Stores the result of x^y` ` ` `res ` `=` `1` ` ` `# Update x if it exceeds mod` ` ` `x ` `=` `x ` `%` `mod` ` ` `while` `(y > ` `0` `):` ` ` `# If y is odd, then multiply` ` ` `# x with result` ` ` `if` `(y & ` `1` `):` ` ` `res ` `=` `(res ` `*` `x) ` `%` `mod` ` ` `# Divide y by 2` ` ` `y ` `=` `y >> ` `1` ` ` `# Update the value of x` ` ` `x ` `=` `(x ` `*` `x) ` `%` `mod` ` ` `# Return the value of x^y` ` ` `return` `res` `# Function to count relations that` `# are irreflexive and antisymmetric` `# in a set consisting of N elements` `def` `numberOfRelations(N):` ` ` `# Return the resultant count` ` ` `return` `power(` `3` `, (N ` `*` `N ` `-` `N) ` `/` `/` `2` `)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `N ` `=` `2` ` ` `print` `(numberOfRelations(N))` ` ` `# This code is contributed by ukasp.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `static` `int` `mod = 1000000007;` `// Function to calculate` `// x ^ y % mod in O(log y)` `static` `int` `power(` `long` `x, ` `int` `y)` `{` ` ` ` ` `// Stores the result of x^y` ` ` `int` `res = 1;` ` ` `// Update x if it exceeds mod` ` ` `x = x % mod;` ` ` `while` `(y > 0)` ` ` `{` ` ` `// If y is odd, then multiply` ` ` `// x with result` ` ` `if` `(y % 2 == 1)` ` ` `res = (` `int` `)(res * x) % mod;` ` ` `// Divide y by 2` ` ` `y = y >> 1;` ` ` `// Update the value of x` ` ` `x = (x * x) % mod;` ` ` `}` ` ` `// Return the value of x^y` ` ` `return` `res;` `}` `// Function to count relations that` `// are irreflexive and antisymmetric` `// in a set consisting of N elements` `static` `int` `numberOfRelations(` `int` `N)` `{` ` ` ` ` `// Return the resultant count` ` ` `return` `power(3, (N * N - N) / 2);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `N = 2;` ` ` `Console.Write(numberOfRelations(N));` `}` `}` `// This code is contributed by Princi Singh` |

## Javascript

`<script>` `// JavaScript program for the above approach` `let mod = 1000000007;` ` ` `// Function to calculate` `// x ^ y % mod in O(log y)` `function` `power(x, y)` `{` ` ` `// Stores the result of x^y` ` ` `let res = 1;` ` ` ` ` `// Update x if it exceeds mod` ` ` `x = x % mod;` ` ` ` ` `while` `(y > 0) {` ` ` ` ` `// If y is odd, then multiply` ` ` `// x with result` ` ` `if` `(y & 1)` ` ` `res = (res * x) % mod;` ` ` ` ` `// Divide y by 2` ` ` `y = y >> 1;` ` ` ` ` `// Update the value of x` ` ` `x = (x * x) % mod;` ` ` `}` ` ` ` ` `// Return the value of x^y` ` ` `return` `res;` `}` ` ` `// Function to count relations that` `// are irreflexive and antisymmetric` `// in a set consisting of N elements` `function` `numberOfRelations(N)` `{` ` ` `// Return the resultant count` ` ` `return` `power(3, (N * N - N) / 2);` `}` ` ` `// Driver Code` ` ` `let N = 2;` ` ` `document.write(numberOfRelations(N));` ` ` `</script>` |

**Output:**

3

**Time Complexity:** O(log N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.