Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number of magic sets from a permutation of first n natural numbers in Python
Suppose we have an array A with first n natural numbers, and one permutation P{p1, p2, ... pn} of array A. We have to check how many magic sets are there. A permutation is said to be magic set, if this satisfies these few rules −
- If we have k, then the elements in positions a[1], a[2], ... a[k] are less than their adjacent elements [P[a[i] - 1] > P[a[i]] < P[a[i] + 1]]
- If we have l, then the elements in positions b[1], b[2], ... b[l] are greater than their adjacent elements [P[b[i] - 1] < P[b[i]] > P[b[i] + 1]]
So, if the input is like n = 4 k = 1 l = 1 k_vals = [2] l_vals = [3], then the output will be 5 because: N = 4, a[1] = 2 and b[1] = 3. So the five permutations are [2,1,4,3], [3,2,4,1], [4,2,3,1], [3,1,4,2], [4,1,3,2].
To solve this, we will follow these steps −
- p := 10^9+7
- F := An array of size n+2 and fill with 0
- for each a in k_vals, do
- if F[a - 1] is 1 or F[a + 1] is 1, then
- if F[a - 1] is 1 or F[a + 1] is 1, then
- p := null
- F[a] := 1
- if F[a - 1] is 1 or F[a + 1] is 1, then
- if F[a - 1] is 1 or F[a + 1] is 1, then
- for each b in l_vals, do
- if F[b] is 1 or F[b - 1] is -1 or F[b + 1] is -1, then
- p := null
- F[b] := -1
- if F[b] is 1 or F[b - 1] is -1 or F[b + 1] is -1, then
- if p is same as null, then
- return 0
- otherwise,
- A := An array of size n+1 and fill with 0
- B := An array of size n+1 and fill with 0
- FF := An array of size n+1 and fill with null
- for i in range 1 to n, do
- FF[i] := F[i] - F[i - 1]
- A[1] := 1
- for i in range 2 to n, do
- for j in range 1 to i, do
- if FF[i] > 0, then
- B[j] :=(B[j - 1] + A[j - 1]) mod p
- otherwise when FF[i] < 0, then
- B[j] :=(B[j - 1] + A[i - 1] - A[j - 1]) mod p
- otherwise,
- B[j] :=(B[j - 1] + A[i - 1]) mod p
- if FF[i] > 0, then
- swap A and B
- for j in range 1 to i, do
- return A[n]
Example
Let us see the following implementation to get better understanding −
def solve(n, k, l, k_vals, l_vals):
p = 10**9+7
F = [0] * (n + 2)
for a in k_vals:
if F[a - 1] == 1 or F[a + 1] == 1:
p = None
F[a] = 1
for b in l_vals:
if F[b] == 1 or F[b - 1] == -1 or F[b + 1] == -1:
p = None
F[b] = -1
if p == None:
return 0
else:
A = [0] * (n + 1)
B = [0] * (n + 1)
FF = [None] * (n + 1)
for i in range(1, n + 1):
FF[i] = F[i] - F[i - 1]
A[1] = 1
for i in range(2, n + 1):
for j in range(1, i + 1):
if FF[i] > 0:
B[j] = (B[j - 1] + A[j - 1]) % p
elif FF[i] < 0:
B[j] = (B[j - 1] + A[i - 1] - A[j - 1]) % p
else:
B[j] = (B[j - 1] + A[i - 1]) % p
A, B = B, A
return A[n]
n = 4
k = 1
l = 1
k_vals = [2]
l_vals = [3]
print(solve(n, k, l, k_vals, l_vals))
Input
4, 1, 1, [2], [3]
Input
5
Advertisements