Saturday, October 5, 2024
Google search engine
HomeData Modelling & AICount of ordered triplets(x, y, z) for a given set of input

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++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
const unsigned int mod = 1000000007;
 
// Function to count the
// total number of possible triplets
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;
}
 
// Driver code
int main()
{
    int N = 3, M = 3, P = 3;
    long long int ans = solve(N, M, P);
    cout << ans << endl;
    return 0;
}


Java




// Java code to implement the above approach
 
// Importing Arrays class from the utility class
import java.util.Arrays;
class GFG
{
 
  public static long mod = 1000000007;
 
  // Function to count the
  // total number of possible triplets
  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;
  }
 
  // Driver method
  public static void main(String[] args)
  {
    int N = 3, M = 3, P = 3;
    long ans = solve(N, M, P);
 
    System.out.println(ans);
 
  }
}
 
// This code is contributed by rakeshsahni


Python3




# Python3 program for the above approach
 
# Function to count the total number of
# possible triplets
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
 
# Driver Code
if __name__ == "__main__":
 
    N, M, P = 3, 3, 3
     
    ans = solve(N, M, P)
    print(ans)
 
# This code is contributed by Abhishek Thakur.


C#




// C# code to implement the above approach
 
// Importing Arrays class from the utility class
using System;
class GFG
{
 
  static long mod = 1000000007;
 
  // Function to count the
  // total number of possible triplets
  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;
  }
 
  // Driver method
  public static void Main()
  {
    int N = 3, M = 3, P = 3;
    long ans = solve(N, M, P);
 
    Console.Write((ans));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




  <script>
      // JavaScript code for the above approach
 
      let mod = 1000000007;
 
      // Function to count the
      // total number of possible triplets
      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;
      }
 
      // Driver code
      let N = 3, M = 3, P = 3;
      let ans = solve(N, M, P);
      document.write(ans + '<br>')
 
// This code is contributed by Potta Lokesh
  </script>


Output

6

Time Complexity: O(1)
Auxiliary Space: O(1)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments