给定一个大小为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
Javascript
4
时间复杂度: O(N * log(N))
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。