Count of ordered triplets(x, y, z) for a given set of input
Given three integers N, M and P. The task is to count the number of possible ordered triplets of form (x, y, z) where
1 ≤ x ≤ N, 1 ≤ y ≤ M and 1 ≤ z ≤ P
Since this count can be very large, return the answer modulo 109 + 7.
Examples:
Input: N = 3, M = 3, P = 3
Output: 6
Explanation: The possible triplets are:
(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)
Input: N = 1, M = 2, P = 3
Output: 1
Explanation: Only one triplet is possible (1, 2, 3)
Approach: The solution is based on the following observation.
- Say after sorting the three numbers in ascending order they are A, B and C respectively.
- So there are A choices to choose first element.
- Now this choice is not available for choosing the second one. For second one there are total (B – 1) choices.
- Now these two choices are not available for the last. So there are only (C – 2) choices for third.
- Therefore the total number of possibilities are A * (B – 1) * (C – 2).
Follow the steps mentioned below to implement the above observation.
- Sort the three inputs in ascending order. Let the sorted order be (N1, N2, N3).
- Now apply the formula derived from the observation to get the final answer.
- Return the final answer modulo 109 + 7.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
const unsigned int mod = 1000000007;
long long int solve( int N, int M, int P)
{
int nums[] = { N, M, P };
sort(nums, nums + 3);
long long int ans = ((nums[0] *
(nums[1] - 1)) % mod
* (nums[2] - 2) %
mod)% mod;
return ans;
}
int main()
{
int N = 3, M = 3, P = 3;
long long int ans = solve(N, M, P);
cout << ans << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
public static long mod = 1000000007 ;
static long solve( int N, int M, int P)
{
int nums[] = { N, M, P };
Arrays.sort(nums);
long ans = ((nums[ 0 ] * (nums[ 1 ] - 1 )) % mod
* (nums[ 2 ] - 2 ) % mod)
% mod;
return ans;
}
public static void main(String[] args)
{
int N = 3 , M = 3 , P = 3 ;
long ans = solve(N, M, P);
System.out.println(ans);
}
}
|
Python3
def solve(N, M, P):
mod = 1000000007
nums = [ N, M, P ]
nums.sort()
ans = ((nums[ 0 ] * (nums[ 1 ] - 1 )) % mod *
(nums[ 2 ] - 2 ) % mod) % mod
return ans
if __name__ = = "__main__" :
N, M, P = 3 , 3 , 3
ans = solve(N, M, P)
print (ans)
|
C#
using System;
class GFG
{
static long mod = 1000000007;
static long solve( int N, int M, int P)
{
int []nums = { N, M, P };
Array.Sort(nums);
long ans = ((nums[0] * (nums[1] - 1)) % mod
* (nums[2] - 2) % mod) % mod;
return ans;
}
public static void Main()
{
int N = 3, M = 3, P = 3;
long ans = solve(N, M, P);
Console.Write((ans));
}
}
|
Javascript
<script>
let mod = 1000000007;
function solve(N, M, P) {
let nums = [N, M, P];
nums.sort( function (a, b) { return a - b })
let ans = ((nums[0] *
(nums[1] - 1)) % mod
* (nums[2] - 2) %
mod) % mod;
return ans;
}
let N = 3, M = 3, P = 3;
let ans = solve(N, M, P);
document.write(ans + '<br>' )
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
04 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...