Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of elements in all permutation which are following given conditions in Python
Suppose we have a set A where all elements from 1 to n are present. And P(A) represents all permutations of elements present in A. We have to find number of elements in P(A) which satisfies the given conditions:
- For all i in range [1, n], A[i] is not same as i
- There exists a set of k indices {i1, i2, ... ik} such that A[ij] = ij+1 for all j < k and A[ik] = i1 (cyclic)
So, if the input is like n = 3 k = 2, then the output will be 0.
Understanding the Problem
Consider the arrays are 1-indexed. As N = 3 and K = 2, we can find 2 sets of A that satisfy the first property a[i] ? i, they are [3,1,2] and [2,3,1]. Now as K = 2, we can have 6 possible cycles of length 2:
[1,2], [1,3], [2,3], [2,1], [3,1], [3,2]
Now let's check if any permutation contains a cycle of length k = 2:
P(A) ? [3,1,2]
- [1,2]: A[1] ? 2
- [1,3]: A[1] = 3 but A[3] ? 1
- [2,3]: A[2] ? 3
- [2,1]: A[2] = 1 but A[1] ? 2
- [3,1]: A[3] = 2 ? 1
- [3,2]: A[3] ? 2
P(A) ? [2,3,1]
- [1,2]: A[1] = 2 but A[2] ? 1
- [1,3]: A[1] ? 3
- [2,3]: A[2] = 3 but A[3] ? 2
- [2,1]: A[2] ? 1
- [3,1]: A[3] = 1 but A[1] ? 3
- [3,2]: A[3] ? 2
As none of the permutations contain a cycle of length k, the result is 0.
Algorithm
To solve this, we will follow these steps:
- Generate all permutations of arrays with elements in range [0, n-1] (0-indexed)
- For each permutation, check if it's a derangement (no element at its original position)
- If it's a derangement, find all cycles and check if any cycle has length k
- Count such valid permutations
Example
Let us see the following implementation to get better understanding:
import itertools
def solve(n, k):
ps = itertools.permutations(range(n), n)
count = 0
for p in ps:
# Check if it's a derangement (no element at its original position)
is_derangement = True
for i, a in enumerate(p):
if a == i:
is_derangement = False
break
if is_derangement:
# Find cycles and check if any cycle has length k
visited = [False] * n
found_cycle_k = False
for j in range(n):
if not visited[j]:
current = j
cycle_length = 0
# Traverse the cycle
while not visited[current]:
visited[current] = True
current = p[current]
cycle_length += 1
if cycle_length == k:
found_cycle_k = True
break
if found_cycle_k:
count += 1
return count
# Test the function
n = 3
k = 2
result = solve(n, k)
print(f"Number of valid permutations: {result}")
The output of the above code is:
Number of valid permutations: 0
How It Works
The algorithm works by:
-
Generating permutations: Uses
itertools.permutations()to generate all possible arrangements - Checking derangement: Ensures no element is at its original position (a[i] ? i)
- Finding cycles: Uses cycle detection to find all cycles in the permutation
- Matching cycle length: Counts permutations that have at least one cycle of length k
Conclusion
This solution efficiently finds permutations that are derangements and contain cycles of a specific length k. The algorithm uses cycle detection to analyze the structure of each valid permutation.
