给定大小为N的数组arr [] ,任务是查找符合给定条件的数组中的排列数:
- 如果K是数组中的最大元素,则数组中K之前的元素应按升序排列,数组K中之后的元素应按降序排列。
例子:
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 decsending 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 decsending order after it.
The permutations which do not satisfy this condition are {1, 1, 2}, {2, 2, 1}.
观察:通过仔细观察,可以得出以下观察结果:
- 可以得出结论,如果任何数字重复两次以上,那么就不会有满足给定条件的排列。这是因为,在所有排列中,这将在最大元素之前或之后被看到两次,从而违反了给定条件。
- 可以得出的第二个观察结果是,数组中的最大元素应该只出现一次。如果出现不止一次,则可能会在最大元素之前看到多余的副本,从而违反了给定条件。
方法:当不违反上述两个观察结果时,则想法是将数组划分为两部分,并在每个分区中填充元素,如下所示:
- 由于我们可以将数组分为两部分。一个在最大元素之前,另一个在最大元素之后。因此,每个元素都有两个选择,是出现在数组中最大元素之外的最大元素之前还是之后。
- 如果任何元素在数组中出现两次,则该元素只有一个选项。它肯定必须在最大元素之前出现一次,并且在最大元素之后出现一次。
- 例如,
- 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.
- 因此,如果N是数组的大小,M是出现次数= 2的数组中元素的数目,则满足条件的排列数将为2 (N –(2 * X)– 1) 。
下面是上述方法的实现:
C++
// C++ program to find the number
// of permutations that satisfy
// the given condition in an array
#include
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
4
时间复杂度: O(N * log(N))