Find the Number of Permutations that satisfy the given condition in an array
Last Updated :
21 Nov, 2021
Given an array arr[] of size N, the task is to find the number of permutations in the array that follows the given condition:
- If K is the maximum element in the array, then the elements before K in the array should be in the ascending order and the elements after K in the array should be in the descending order.
Examples:
Input: arr[] = {1, 2, 3}
Output: 4
Explanation:
There are a total of 6 permutations for the given array {1, 2, 3}. They are:
{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, and {3, 2, 1}
Out of the above permutations, only {1, 2, 3}, {1, 3, 2}, {2, 3, 1}, {3, 2, 1} are the arrays which follow the strictly ascending order before the maximum element 3 and strictly descending order after it.
The permutations which do not satisfy this condition are {2, 1, 3}, {3, 1, 2}.
Input: arr[] = {1 1 2}
Output: 1
There are a total of 3 permutations for the given array {1, 1, 2}. They are:
{1 1 2}, {1 2 1} and {2 1 1}
Out of the above permutations, only {1, 2, 1} is the array which follow the strictly ascending order before the maximum element 2 and strictly descending order after it.
The permutations which do not satisfy this condition are {1, 1, 2}, {2, 2, 1}.
Observations: On observing carefully, the following observations can be made:
- It can be concluded that if any number repeats more than twice, then there will be no permutation which satisfies the given condition. This is because, in all the permutations, this will be seen twice either before the maximum element or after the maximum element thus violating the given condition.
- The second observation which can be made is that the maximum element in the array should appear only once. If it appears more than once, then the extra copies may be seen before the maximum element thereby violating the given condition.
Approach: When the above two observations are not violated, then the idea is to partition the array into two parts and fill elements in each partition as:
- Since we can partition the array into two parts. One is before the maximum element and the other one is after the maximum element. Therefore, every element has two choices whether to appear either before the maximum element or after the maximum element except the maximum element in the array.
- If any element appears twice in the array, then that element has only one option. It definitely has to appear once before the maximum element and once after the maximum element.
- For example,
- If arr[] = {1, 2, 2, 3, 4}, the maximum element 4 has only one occurrence and no element occurs more than twice.
- Now, the array can be divided into two parts: { }4{ } with 4 being the maximum element.
- Since 2 is repeated twice, it should be available on both the sides (i.e.) {2}4{2}.
- 1 and 3 both have two choices for the left part and right part. Therefore, there are 2 * 2 = 4 possible permutations.
- Therefore, if N is the size of the array and M is the number of elements in the array with its occurrence = 2, then the number of permutations satisfying the condition will be 2(N - (2 * X) - 1).
Below is the implementation of the above approach:
C++
// C++ program to find the number
// of permutations that satisfy
// the given condition in an array
#include <bits/stdc++.h>
using namespace std;
// Function to calculate x ^ y
// recursively
int pow(int x, int y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
int temp = pow(x, y / 2);
temp *= temp;
if (y & 1)
temp *= x;
return temp;
}
// Function to return the number of
// permutations that satisfy the
// given condition in an array
int noOfPermutations(int* a, int n)
{
// If there is only one element then
// only one permutation is available
if (n == 1) {
return 1;
}
// Sort the array for calculating
// the number of elements occurring twice
sort(a, a + n);
// If the maximum element is occurring
// twice, then the number of permutations
// satisfying the condition is 0
if (a[n - 1] == a[n - 2]) {
return 0;
}
// This variable will store the
// number of element occurring twice
int x = 0;
// Loop to check the number of elements
// occurring twice
for (int i = 0; i < n - 2; ++i) {
// Check if this element
// is occurring twice
if (a[i] == a[i + 1]) {
// If this element is occurring
// twice then check if this number
// is occurring more than twice
if (a[i] == a[i + 2]) {
// If element occurring thrice
// then no permutation will
// satisfy the given condition
return 0;
}
x++;
// Since we have checked the next
// element as well, then we can
// increment the loop variable
i++;
}
}
return pow(2, n - 2 * x - 1);
}
// Driver code
int main()
{
int a[] = { 1, 2, 2, 3, 4 };
int n = sizeof(a) / sizeof(a[0]);
int num = noOfPermutations(a, n);
cout << num;
return 0;
}
Java
// Java program to find the number
// of permutations that satisfy
// the given condition in an array
import java.util.*;
class GFG{
// Function to calculate x ^ y
// recursively
static int pow(int x, int y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
int temp = pow(x, y / 2);
temp *= temp;
if (y % 2 == 1)
temp *= x;
return temp;
}
// Function to return the number of
// permutations that satisfy the
// given condition in an array
static int noOfPermutations(int []a, int n)
{
// If there is only one element then
// only one permutation is available
if (n == 1) {
return 1;
}
// Sort the array for calculating
// the number of elements occurring twice
Arrays.sort(a);
// If the maximum element is occurring
// twice, then the number of permutations
// satisfying the condition is 0
if (a[n - 1] == a[n - 2]) {
return 0;
}
// This variable will store the
// number of element occurring twice
int x = 0;
// Loop to check the number of elements
// occurring twice
for (int i = 0; i < n - 2; ++i) {
// Check if this element
// is occurring twice
if (a[i] == a[i + 1]) {
// If this element is occurring
// twice then check if this number
// is occurring more than twice
if (a[i] == a[i + 2]) {
// If element occurring thrice
// then no permutation will
// satisfy the given condition
return 0;
}
x++;
// Since we have checked the next
// element as well, then we can
// increment the loop variable
i++;
}
}
return pow(2, n - 2 * x - 1);
}
// Driver code
public static void main(String[] args)
{
int a[] = { 1, 2, 2, 3, 4 };
int n = a.length;
int num = noOfPermutations(a, n);
System.out.print(num);
}
}
// This code is contributed by 29AjayKumar
Python 3
# Python 3 program to find the number
# of permutations that satisfy
# the given condition in an array
# Function to calculate x ^ y
# recursively
def pow( x, y):
if (y == 1):
return x
if (y == 0):
return 1
temp = pow(x, y // 2)
temp *= temp
if (y & 1):
temp *= x
return temp
# Function to return the number of
# permutations that satisfy the
# given condition in an array
def noOfPermutations(a, n):
# If there is only one element then
# only one permutation is available
if (n == 1):
return 1
# Sort the array for calculating
# the number of elements occurring twice
a.sort()
# If the maximum element is occurring
# twice, then the number of permutations
# satisfying the condition is 0
if (a[n - 1] == a[n - 2]):
return 0
# This variable will store the
# number of element occurring twice
x = 0
# Loop to check the number of elements
# occurring twice
for i in range( n - 2):
# Check if this element
# is occurring twice
if (a[i] == a[i + 1]):
# If this element is occurring
# twice then check if this number
# is occurring more than twice
if (a[i] == a[i + 2]):
# If element occurring thrice
# then no permutation will
# satisfy the given condition
return 0
x += 1
# Since we have checked the next
# element as well, then we can
# increment the loop variable
i += 1
return pow(2, n - 2 * x - 1)
# Driver code
if __name__ == "__main__":
a = [ 1, 2, 2, 3, 4 ]
n = len(a)
num = noOfPermutations(a, n)
print (num)
# This code is contributed by chitranayal
C#
// C# program to find the number
// of permutations that satisfy
// the given condition in an array
using System;
class GFG{
// Function to calculate x ^ y
// recursively
static int pow(int x, int y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
int temp = pow(x, y / 2);
temp *= temp;
if (y % 2 == 1)
temp *= x;
return temp;
}
// Function to return the number of
// permutations that satisfy the
// given condition in an array
static int noOfPermutations(int []a, int n)
{
// If there is only one element then
// only one permutation is available
if (n == 1) {
return 1;
}
// Sort the array for calculating
// the number of elements occurring twice
Array.Sort(a);
// If the maximum element is occurring
// twice, then the number of permutations
// satisfying the condition is 0
if (a[n - 1] == a[n - 2]) {
return 0;
}
// This variable will store the
// number of element occurring twice
int x = 0;
// Loop to check the number of elements
// occurring twice
for (int i = 0; i < n - 2; ++i) {
// Check if this element
// is occurring twice
if (a[i] == a[i + 1]) {
// If this element is occurring
// twice then check if this number
// is occurring more than twice
if (a[i] == a[i + 2]) {
// If element occurring thrice
// then no permutation will
// satisfy the given condition
return 0;
}
x++;
// Since we have checked the next
// element as well, then we can
// increment the loop variable
i++;
}
}
return pow(2, n - 2 * x - 1);
}
// Driver code
public static void Main(String[] args)
{
int []a = { 1, 2, 2, 3, 4 };
int n = a.Length;
int num = noOfPermutations(a, n);
Console.Write(num);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to find the number
// of permutations that satisfy
// the given condition in an array
// Function to calculate x ^ y
// recursively
function pow(x, y)
{
if (y == 1)
return x;
if (y == 0)
return 1;
let temp = pow(x, y / 2);
temp *= temp;
if (y % 2 == 1)
temp *= x;
return temp;
}
// Function to return the number of
// permutations that satisfy the
// given condition in an array
function noOfPermutations(a, n)
{
// If there is only one element then
// only one permutation is available
if (n == 1) {
return 1;
}
// Sort the array for calculating
// the number of elements occurring twice
a.sort();
// If the maximum element is occurring
// twice, then the number of permutations
// satisfying the condition is 0
if (a[n - 1] == a[n - 2]) {
return 0;
}
// This variable will store the
// number of element occurring twice
let x = 0;
// Loop to check the number of elements
// occurring twice
for (let i = 0; i < n - 2; ++i) {
// Check if this element
// is occurring twice
if (a[i] == a[i + 1]) {
// If this element is occurring
// twice then check if this number
// is occurring more than twice
if (a[i] == a[i + 2]) {
// If element occurring thrice
// then no permutation will
// satisfy the given condition
return 0;
}
x++;
// Since we have checked the next
// element as well, then we can
// increment the loop variable
i++;
}
}
return pow(2, n - 2 * x - 1);
}
// Driver code
let a = [ 1, 2, 2, 3, 4 ];
let n = a.length;
let num = noOfPermutations(a, n);
document.write(num);
</script>
Time Complexity: O(N * log(N))
Auxiliary Space: O(log y)
Similar Reads
Count of triplets in an array that satisfy the given conditions Given an array arr[] of N elements, the task is to find the count of triplets (arr[i], arr[j], arr[k]) such that (arr[i] + arr[j] + arr[k] = L) and (L % arr[i] = L % arr[j] = L % arr[k] = 0.Examples: Input: arr[] = {2, 4, 5, 6, 7} Output: 1 Only possible triplet is {2, 4, 6}Input: arr[] = {4, 4, 4,
13 min read
Count possible permutations of given array satisfying the given conditions Given an array, arr[] consisting of N distinct elements, the task is to count possible permutations of the given array that can be generated which satisfies the following properties: The two halves must be sorted.arr[i] must be less than arr[N / 2 + i] Note: N is always even and indexing starts from
6 min read
Change the array into a permutation of numbers from 1 to n Given an array A of n elements. We need to change the array into a permutation of numbers from 1 to n using minimum replacements in the array. Examples: Input : A[] = {2, 2, 3, 3} Output : 2 1 3 4 Explanation: To make it a permutation of 1 to 4, 1 and 4 are missing from the array. So replace 2, 3 wi
5 min read
Count number of coordinates from an array satisfying the given conditions Given an array arr[] consisting of N coordinates in the Cartesian Plane, the task is to find the number of coordinates, such as (X, Y), that satisfies all the following conditions: All possible arr[i][0] must be less than X and arr[i][1] must be equal to Y.All possible arr[i][0] must be greater than
10 min read
Find the number of sub arrays in the permutation of first N natural numbers such that their median is M Given an array arr[] containing the permutation of first N natural numbers and an integer M ? N. The task is to find the number of sub-arrays such that the median of the sequence is M. The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non
7 min read