# Count of subarrays of size K which is a permutation of numbers from 1 to K

Given an array **arr** of distinct integers, the task is to find count of sub-arrays of size **i** having all elements from** 1 to i**, in other words the sub-array is any permutation of elements from **1 to i**, with **1 < = i <= N**.

**Examples:**

Input:arr[] = {2, 3, 1, 5, 4}

Output:3

Explanation:

we have {1}, {2, 3, 1} and {2, 3, 1, 5, 4} subarray for i=1, i=3, i=5 respectively.

Permutation of size 4 and size 2 can’t be made because 5 and 3 are in the way respectively.

Input:arr[] = {1, 3, 5, 4, 2}

Output:2

Explanation:

we have {1} and {1, 3, 5, 4, 2} subarray for i=1 and i=5 respectively.

A **Naive approach** is to start from each index and try to find subarray of every size(i) and check whether all elements from 1 to i are present.

*Time complexity: O(N ^{2})*

An **Efficient approach** can be given by checking if it is possible to create a subarray of size **i** for every value of i from **1 to N**.

As we know every subarray of size **K** must be a permutation of all elements from **1 to K**, knowing that we can look at the index of the numbers from **1 to N** in order and calculate the index of the minimum and maximum value at every step.

- If
**maximum_ind – minimum_ind + 1 = K**, then we have a permutation of size K, else not. - Update the value of minimum_ind and maximum_ind at every step.

**Time complexity:** O(n)

**Illustration:**

Given Arr =

{2, 3, 1, 5, 4}, let’s start with min_ind = INF and max_ind = -1

- index of 1 is 2, so min_ind = min(min_ind, 2) = 2 and max_ind = max(max_ind, 2) = 2,

2-2+1 = 1 so we have a permutation of size 1- index of 2 is 0, so min_ind = min(min_ind, 0) = 0 and max_ind = max(max_ind, 0) = 2,

2-0+1 = 3 so we don’t have a permutation of size 2- index of 3 is 1, so min_ind = min(min_ind, 1) = 0 and max_ind = max(max_ind, 1) = 2,

2-0+1 = 3 so we have a permutation of size 3- index of 4 is 4, so min_ind = min(min_ind, 4) = 0 and max_ind = max(max_ind, 4) = 4,

4-0+1 = 5 so we don’t have a permutation of size 4- index of 5 is 3, so min_ind = min(min_ind, 3) = 0 and max_ind = max(max_ind, 4) = 4,

4-0+1 = 5 so we have a permutation of size 5So answer is 3

Below is implementation of the above approach:

## C++

`// C++ program to implement ` `// the above approach ` ` ` `#include <iostream> ` `#include <unordered_map> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `int` `find_permutations(vector<` `int` `>& arr) ` `{ ` ` ` `int` `cnt = 0; ` ` ` `int` `max_ind = -1, min_ind = 10000000; ` ` ` `int` `n = arr.size(); ` ` ` `unordered_map<` `int` `, ` `int` `> index_of; ` ` ` ` ` `// Save index of numbers of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `index_of[arr[i]] = i + 1; ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// Update min and max index ` ` ` `// with the current index ` ` ` `// and check if it's a valid permutation ` ` ` `max_ind = max(max_ind, index_of[i]); ` ` ` `min_ind = min(min_ind, index_of[i]); ` ` ` `if` `(max_ind - min_ind + 1 == i) ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> nums; ` ` ` `nums.push_back(2); ` ` ` `nums.push_back(3); ` ` ` `nums.push_back(1); ` ` ` `nums.push_back(5); ` ` ` `nums.push_back(4); ` ` ` ` ` `cout << find_permutations(nums); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Count non decreasing subarrays of size N from N Natural numbers
- Count of subarrays of size K with elements having even frequencies
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Minimum count of increment of K size subarrays required to form a given Array
- Count of subarrays having exactly K prime numbers
- Count subarrays with sum as difference of squares of two numbers
- Count of total subarrays whose sum is a Fibonacci Numbers
- Count of subarrays having exactly K perfect square numbers
- Count of subarrays whose product is equal to difference of two different numbers
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Find the count of natural Hexadecimal numbers of size N
- Generate an array of given size with equal count and sum of odd and even numbers
- Sum of all subarrays of size K
- Max sum of M non-overlapping subarrays of size K
- Maximum of all Subarrays of size k using set in C++ STL
- Counting inversions in all subarrays of given size
- Minimum and Maximum of all subarrays of size K using Map
- Maximum sum two non-overlapping subarrays of given size

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.