# Count of triplets of numbers 1 to N such that middle element is always largest

Given an integer **N**, the task is to count the number of ways to arrange triplets (**a**, **b**, **c**) within **[1, N]** in such a way such that the middle element is always greater than left and right elements.

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

**Naive Approach:** Check for all triplets whetehr it satisfies the given condition using three nested loops and keep incrementing their count every time a triplet satisfies the condition.

**Time Complexity:** *O( N ^{3} )*

**Auxillary Space:**

*O( 1 )*

**Efficient Approach:**

- Check all the possibilities for middle element and try to find the number of possible arrangements keeping each of them fixed one by one.
- We can observe that all the numbers between
**[3, N]**can occupy the middle slot.

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

Below is the implementation of the above approach.

## C++

`// C++ program to implement ` `// the above approch ` ` ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approch ` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approch ` ` ` `# 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 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approch ` `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 ` |

*chevron_right*

*filter_none*

**Output:**

240

**Time Complexity:** *O(1)*

**Auxillary Space:** *O(1)*

## Recommended Posts:

- Count of triplets from the given Array such that sum of any two elements is the third element
- Count Triplets such that one of the numbers can be written as sum of the other two
- Largest element smaller than current element on left for every element in Array
- Maximum possible middle element of the array after deleting exactly k elements
- Count the triplets such that A[i] < B[j] < C[k]
- Count of triplets in a given Array having GCD K
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Count triplets with sum smaller than a given value
- Count triplets in an array such that i<j<k and a[j] - a[i] = a[k] - a[j] = D
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Replace elements with absolute difference of smallest element on left and largest element on right
- Count of triplets that satisfy the given equation
- Count triplets such that sum of any two number is equal to third | Set 2
- Count non-negative triplets with sum equal to N
- Count of triplets in an array that satisfy the given conditions
- Count of ordered triplets (R, G, B) in a given original string
- Count ways to form minimum product triplets
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Array formed from difference of each element from the largest element in the given array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.