Count of subarrays which forms a permutation from given Array elements
Given an array A[] consisting of integers [1, N], the task is to count the total number of subarrays of all possible lengths x (1 ? x ? N), consisting of a permutation of integers [1, x] from the given array.
Examples:
Input: A[] = {3, 1, 2, 5, 4} Output: 4
Explanation:
Subarrays forming a permutation are {1}, {1, 2}, {3, 1, 2} and {3, 1, 2, 5, 4}.
Input: A[] = {4, 5, 1, 3, 2, 6} Output: 4
Explanation:
Subarrays forming a permutation are {1}, {1, 3, 2}, {4, 5, 1, 3, 2} and {4, 5, 1, 3, 2, 6}.
Naive Approach:
Follow the steps below to solve the problem:
- The simplest approach to solve the problem is to generate all possible subarrays.
- For each subarray, check if it is a permutation of elements in the range [1, length of subarray].
- For every such subarray found, increase count. Finally, print the count.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach:
To optimize the above approach, follow the steps below:
- For every element from i = [1, N], check the maximum and minimum index, at which the elements of the permutation [1, i] are present.
- If the difference between the maximum and minimum index is equal to i, then it means there is a valid contiguous permutation for i.
- For every such permutation, increase the count. Finally, print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int PermuteTheArray( int A[], int n)
{
int arr[n];
for ( int i = 0; i < n; i++) {
arr[A[i] - 1] = i;
}
int mini = n, maxi = 0;
int count = 0;
for ( int i = 0; i < n; i++) {
mini = min(mini, arr[i]);
maxi = max(maxi, arr[i]);
if (maxi - mini == i)
count++;
}
return count;
}
int main()
{
int A[] = { 4, 5, 1, 3, 2, 6 };
cout << PermuteTheArray(A, 6);
return 0;
}
|
Java
class GFG{
static int PermuteTheArray( int A[], int n)
{
int []arr = new int [n];
for ( int i = 0 ; i < n; i++)
{
arr[A[i] - 1 ] = i;
}
int mini = n, maxi = 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
if (maxi - mini == i)
count++;
}
return count;
}
public static void main(String[] args)
{
int A[] = { 4 , 5 , 1 , 3 , 2 , 6 };
System.out.print(PermuteTheArray(A, 6 ));
}
}
|
Python3
def PermuteTheArray(A, n):
arr = [ 0 ] * n
for i in range (n):
arr[A[i] - 1 ] = i
mini = n
maxi = 0
count = 0
for i in range (n):
mini = min (mini, arr[i])
maxi = max (maxi, arr[i])
if (maxi - mini = = i):
count + = 1
return count
if __name__ = = "__main__" :
A = [ 4 , 5 , 1 , 3 , 2 , 6 ]
print (PermuteTheArray(A, 6 ))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int PermuteTheArray( int []A, int n)
{
int []arr = new int [n];
for ( int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
int mini = n, maxi = 0;
int count = 0;
for ( int i = 0; i < n; i++)
{
mini = Math.Min(mini, arr[i]);
maxi = Math.Max(maxi, arr[i]);
if (maxi - mini == i)
count++;
}
return count;
}
public static void Main(String[] args)
{
int []A = { 4, 5, 1, 3, 2, 6 };
Console.Write(PermuteTheArray(A, 6));
}
}
|
Javascript
<script>
function PermuteTheArray(A, n)
{
var arr = Array(n);
for ( var i = 0; i < n; i++) {
arr[A[i] - 1] = i;
}
var mini = n, maxi = 0;
var count = 0;
for ( var i = 0; i < n; i++) {
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
if (maxi - mini == i)
count++;
}
return count;
}
var A = [4, 5, 1, 3, 2, 6];
document.write( PermuteTheArray(A, 6));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...