- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

# Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python

Suppose we have an array A containing the permutation of first N natural numbers and another number M is also given, where M ≤ N, we have to find the number of sub-arrays such that the median of the sequence is M. As we know the median of a sequence is defined as the value of the element which is in the middle of the sequence after sorting it according to ascending order. For even length sequence, the left of two middle elements is used.

So, if the input is like A = [3, 5, 6, 4, 2] and M = 5, then the output will be 4 as the required subarrays are [3, 5, 6], [5], [5, 6] and [5, 6, 4].

To solve this, we will follow these steps −

n := size of arr

my_map := a new map

my_map[0] := 1

has := False, add := 0, result := 0

for i in range 0 to n, do

if arr[i] < m, then

add := add - 1

otherwise when arr[i] > m, then

add := add + 1

if arr[i] is same as m, then

has := True

if has is true, then

if add present in my_map, then

result := result + my_map[add]

if add-1 present in my_map , then

result := result + my_map[add - 1]

otherwise,

my_map[add] := (value of my_map[add], if present, otherwise 0) + 1

return result

## Example

Let us see the following implementation to get better understanding −

def solve(arr, m): n = len(arr) my_map = {} my_map[0] = 1 has = False add = 0 result = 0 for i in range(n): if (arr[i] < m): add -= 1 elif (arr[i] > m): add += 1 if (arr[i] == m): has = True if (has): if(add in my_map): result += my_map[add] if add-1 in my_map: result += my_map[add - 1] else: my_map[add] = my_map.get(add, 0) + 1 return result arr = [3, 5, 6, 4, 2] m = 5 print(solve(arr, m))

## Input

[3, 5, 6, 4, 2] , 5

## Output

3

- Related Questions & Answers
- 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 maximum N such that the sum of square of first N natural numbers is not more than X in Python
- Find m-th summation of first n natural numbers in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Find the average of first N natural numbers in C++
- Find if given number is sum of first n natural numbers in C++
- Check if product of first N natural numbers is divisible by their sum in Python
- PHP program to find the average of the first n natural numbers that are even
- 8085 program to find the sum of first n natural numbers
- Program to find number m such that it has n number of 0s at end in Python
- PHP program to find the sum of cubes of the first n natural numbers
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++