Count of subarrays which forms a permutation from given Array elements
Last Updated :
17 May, 2021
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++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function returns the required count
int PermuteTheArray(int A[], int n)
{
int arr[n];
// Store the indices of the
// elements present in A[].
for (int i = 0; i < n; i++) {
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
int mini = n, maxi = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = min(mini, arr[i]);
maxi = max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
int main()
{
int A[] = { 4, 5, 1, 3, 2, 6 };
cout << PermuteTheArray(A, 6);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function returns the required count
static int PermuteTheArray(int A[], int n)
{
int []arr = new int[n];
// Store the indices of the
// elements present in A[].
for(int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
int mini = n, maxi = 0;
int count = 0;
for(int i = 0; i < n; i++)
{
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 4, 5, 1, 3, 2, 6 };
System.out.print(PermuteTheArray(A, 6));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to implement
# the above approach
# Function returns the required count
def PermuteTheArray(A, n):
arr = [0] * n
# Store the indices of the
# elements present in A[].
for i in range(n):
arr[A[i] - 1] = i
# Store the maximum and
# minimum index of the
# elements from 1 to i.
mini = n
maxi = 0
count = 0
for i in range(n):
# Update maxi and mini, to
# store minimum and maximum
# index for permutation
# of elements from 1 to i+1
mini = min(mini, arr[i])
maxi = max(maxi, arr[i])
# If difference between maxi
# and mini is equal to i
if (maxi - mini == i):
# Increase count
count += 1
# Return final count
return count
# Driver Code
if __name__ == "__main__":
A = [ 4, 5, 1, 3, 2, 6 ]
print(PermuteTheArray(A, 6))
# This code is contributed by chitranayal
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function returns the required count
static int PermuteTheArray(int []A, int n)
{
int []arr = new int[n];
// Store the indices of the
// elements present in []A.
for(int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
int mini = n, maxi = 0;
int count = 0;
for(int i = 0; i < n; i++)
{
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = Math.Min(mini, arr[i]);
maxi = Math.Max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
public static void Main(String[] args)
{
int []A = { 4, 5, 1, 3, 2, 6 };
Console.Write(PermuteTheArray(A, 6));
}
}
// This code is contributed by gauravrajput1
JavaScript
<script>
// Javascript Program to implement
// the above approach
// Function returns the required count
function PermuteTheArray(A, n)
{
var arr = Array(n);
// Store the indices of the
// elements present in A[].
for (var i = 0; i < n; i++) {
arr[A[i] - 1] = i;
}
// Store the maximum and
// minimum index of the
// elements from 1 to i.
var mini = n, maxi = 0;
var count = 0;
for (var i = 0; i < n; i++) {
// Update maxi and mini, to
// store minimum and maximum
// index for permutation
// of elements from 1 to i+1
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
// If difference between maxi
// and mini is equal to i
if (maxi - mini == i)
// Increase count
count++;
}
// Return final count
return count;
}
// Driver Code
var A = [4, 5, 1, 3, 2, 6];
document.write( PermuteTheArray(A, 6));
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Check if Array elements of given range form a permutation Given an array arr[] consisting of N distinct integers and an array Q[][2] consisting of M queries of the form [L, R], the task for each query is to check if array elements over the range [L, R] forms a permutation or not. Note: A permutation is a sequence of length N containing each number from 1 t
13 min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Count of Derangements of given Array with Repetition Given an array arr[] of N numbers (N ⤠20), the task is to find the number of Derangements of the array where array elements can be repeated. A derangement is a permutation of N elements, such that no element appears in its original position. For example, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0
13 min read
Count of subarrays which start and end with the same element Given an array A of size N where the array elements contain values from 1 to N with duplicates, the task is to find the total number of subarrays that start and end with the same element. Examples: Input: A[] = {1, 2, 1, 5, 2} Output: 7 Explanation: Total 7 sub-array of the given array are {1}, {2},
10 min read
Count non-overlapping Subarrays of size K with equal alternate elements Given an array arr[] of length N, the task is to find the count of non-overlapping subarrays of size K such that the alternate elements are equal. Examples: Input: arr[] = {2, 4, 2, 7}, K = 3Output: 1Explanation: Given subarray {2, 4, 2} is a valid array because the elements in even position(index n
7 min read