给定整数N ,任务是计算在[1,N]内以三元组( a , b , c )排列方式的数量,以使中间元素始终大于左右元素。
Example:
Input: N = 4
Output: 8
Explaination
For the given input N = 4 number of possible triplets are:{1, 3, 2}, {2, 3, 1}, {2, 4, 3}, {3, 4, 2}, {1, 4, 3}, {3, 4, 1}, {2, 4, 1}, {1, 4, 2}.
Input: 10
Output: 240
天真的方法:使用三个嵌套循环检查是否满足给定条件的所有三元组,并在每次三元组满足条件时不断增加其计数。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:
- 检查中间元素的所有可能性,并尝试找到可能的布置的数量,使每个元素一个接一个地固定。
- 我们可以观察到[3,N]之间的所有数字都可以占据中间位置。
Possible arrangements for every middle element:
On placing 3 at the middle, only 2 ( = 2 * 1) possible arrangements exist {1, 3, 2} and {2, 3, 1}.
On placing 4 at the middle, 6 ( = 3 * 2) possible arrangements exist {1, 4, 3}, {1, 4, 2}, {2, 4, 1}, {2, 4, 3}, {3, 4, 1} and {3, 4, 2}.
On placing 5 at the middle, 12 ( = 4 * 3) possible arrangements exist.
.
.
.
On placing N – 1 at the middle, (N-2) * (N-3) possible arrangements exist.
On placing N at the middle, (N-1) * (N-2) possible arrangements exist.
Thus, Total possible arrangements = ( N * (N-1) * (N-2)) / 3
下面是上述方法的实现。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find Number of triplets
// for given Number N such that
// middle element is always greater
// than left and right side element.
int findArrangement(int N)
{
// check if arrangement is
// possible or Not
if (N < 3)
return 0;
// else return total ways
return ((N) * (N - 1) * (N - 2)) / 3;
}
// Driver code.
int main()
{
int N = 10;
cout << findArrangement(N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find number of triplets
// for given number N such that middle
// element is always greater than left
// and right side element.
static int findArrangement(int N)
{
// Check if arrangement
// is possible or not
if (N < 3)
return 0;
// Else return total ways
return ((N) * (N - 1) * (N - 2)) / 3;
}
// Driver code
public static void main(String[] args)
{
int N = 10;
System.out.println(findArrangement(N));
}
}
// This code is contributed by coder001
Python3
# Python3 program to implement
# the above approach
# Function to find Number of triplets
# for given Number N such that middle
# element is always greater than left
# and right side element.
def findArrangement(N):
# Check if arrangement is
# possible or Not
if (N < 3):
return 0;
# Else return total ways
return ((N) * (N - 1) * (N - 2)) // 3;
# Driver code.
N = 10;
print(findArrangement(N));
# This code is contributed by Akanksha_Rai
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find number of triplets
// for given number N such that middle
// element is always greater than left
// and right side element.
static int findArrangement(int N)
{
// Check if arrangement
// is possible or not
if (N < 3)
return 0;
// Else return total ways
return ((N) * (N - 1) * (N - 2)) / 3;
}
// Driver code
public static void Main()
{
int N = 10;
Console.Write(findArrangement(N));
}
}
// This code is contributed by Code_Mech
240
时间复杂度: O(1)
辅助空间: O(1)