- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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

- Related Articles
- Find the good permutation of first N natural numbers C++
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- Program to find sum of first n natural numbers in C++
- Possible two sets from first N natural numbers difference of sums as D in C++
- 8085 program to find the sum of first n natural numbers
- Python Program for cube sum of first n natural numbers
- Python Program for Sum of squares of first n natural numbers
- Sum of first n natural numbers in C Program
- Minimum number of prefix reversals to sort permutation of first N numbers in C++
- Java Program to cube sum of first n natural numbers
- PHP program to find the sum of cubes of the first n natural numbers
- Find if given number is sum of first n natural numbers in C++
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python

Advertisements