Count of triplets of numbers 1 to N such that middle element is always largest
Last Updated :
09 Feb, 2022
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
Explanation
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 whether it satisfies the given condition using three nested loops and keep incrementing their count every time a triplet satisfies the condition.
Time Complexity: O( N3 )
Auxiliary 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 approach
#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;
}
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
JavaScript
<script>
// Javascript 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.
function 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
let N = 10;
document.write(findArrangement(N));
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Number of ways to change the Array such that largest element is LCM of array Given an array arr[] , the task is to count the number of the unique arrays can be formed by updating the elements of the given array to any element in the range [1, arr[i]] such that the Least common multiple of the updated array is equal to the maximum element. Examples: Input: arr[] = {6, 3} Outp
15 min read
Count triplets such that sum of any two number is equal to third Given an array of distinct positive integers arr[] of length n, the task is to count all the triplets such that the sum of two elements equals the third element.Examples: Input: arr[] = [1, 5, 3, 2] Output: 2 Explanation: In the given array, there are two such triplets such that sum of the two numbe
7 min read
Count prime triplets upto N having sum of first two elements equal to the third number Given an integer N, the task is to find the number of distinct prime triplets from the range [1, N] having the sum of the first two numbers equal to the third number. Examples: Input: N = 7Output: 2Explanation: All valid triplets are (2, 3, 5) and (2, 5, 7). Therefore, the required output is 2. Inpu
6 min read
Count of triplets from the given Array such that sum of any two elements is the third element Given an unsorted array arr, the task is to find the count of the distinct triplets in which the sum of any two elements is the third element. Examples: Input: arr[] = {1, 3, 4, 15, 19} Output: 2 Explanation: In the given array there are two triplets such that the sum of the two elements is equal to
7 min read
Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n Given an integer N, the task is to count the number of triplets (a, b, c) such that a2 + b2 = c2 and 1 <= a <= b <= c <= N. Examples: Input: N = 5 Output: 1 The only possible triplet pair is (3, 4, 5) 3^2 + 4^2 = 5^2 i.e. 9 + 16 = 25 Input: N = 10 Output: 2 (3, 4, 5) and (6, 8, 10) are t
12 min read