How to print ordered triplets of natural numbers java

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    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)


    How to print triplets in Java?

    Take three pointers i, j, k..
    Initialize i with zero and start a nested loop for i..
    Initialize j with (i+1) and start a nested loop for j..
    Initialize k with (j+1) and start a loop for k..
    If Target == arr[i] + arr[j] + arr[k] break the loop and print values of arr[i], arr[j], arr[k]..

    How do you find the number of ordered triplets?

    The number of ordered triplets of non-negative integers which are solutions of the equation x + y + z = 100 is..
    The number of ordered triplets of positive integers which are solutions of the equation x+y+z=100 is..
    the number of ordered triplets(x,y,z) such that (x+y)(y+z)(z+x)=2013, where x,y,z are integers, is..

    How do you write ordered triplets?

    Ordered Triples An ordered triple is a list of 3 elements written in a certain order. As with ordered pairs, order is important. For example, the numbers 1, 2 and 3 can form 6 ordered triples: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1).

    How to get triplets from array Java?

    Triplets can be found using the hashing technique..
    Traverse the array from i = 0 to n - 2..
    Create an empty hash table..
    Traverse from j = i+ 1 to n -1..
    sum = arr[i] + arr[j].
    if (-sum) is present in the hash table,.
    then print arr[i], arr[j] and -sum as triplets..
    else, insert arr[j] in the hash table and proceed..