Open In App

Maximize count of increasing triplets from any permutation of given 3 Arrays

Last Updated : 20 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given three arrays X[], Y[], and Z[] each consisting of N integers, the task is to find the maximum number of triplets (X[i], Y[i], Z[i]) such that (X[i] < Y[i] < Z[i]) for any permutation of the three arrays.

Examples:

Input: X = {9, 6, 14, 1,  8}, Y = {2, 10, 3, 12, 11}, Z = {15, 13, 5, 7, 4}
Output: 3
Explanation:  
After rearranging the arrays X[], Y[] and Z[] as {1, 6, 8, 9, 14}, {3, 2, 10, 12, 11}, and {4, 7, 15, 13, 5} respectively. The increasing triplets are {1, 3, 4}, {8, 10, 15} and {9, 12, 13}.
Therefore, the total count of such triplets is 3.

Input: X = {1, 2, 3, 4}, Y = {5, 6, 7, 8}, Z = {9, 10, 11, 12}
Output: 4

Naive Approach: The given problem can be solved by generating all possible combinations of triplets of the three arrays and count those triplets that satisfy the given conditions. After checking for all the permutations, print the total count of triplets obtained.

Time Complexity: O(N*(N!)3)
Auxiliary Space: O(1)

Efficient Approach: The given problem can be solve by using the Greedy Approach, the idea is to sort the given array X[] and then for finding the triplets choose those elements in the array Y[] and Z[] that forms increasing triplets for every elements of the array and this idea can be implemented using the priority queue. Follow the steps below to solve the problem:

  • Sort the array X[] in increasing order.
  • Initialize two priority queues, say PQY and PQZ implementing MinHeap for the array Y[] and Z[] respectively.
  • Store all the element of the array Y[] in the PQY.
  • Store all the element of the array Z[] in the PQZ.
  • Traverse the array X[] and perform the following steps:
  • After completing the above steps, print the value of count as the resultant maximum count of triplets.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of triplets
// that are in increasing order from the
// given three arrays after rearranging
int countTriplet(int arr1[], int arr2[],
                 int arr3[], int N)
{
 
    // Sort the given array arr[]
    sort(arr1, arr1 + N);
 
    // Initializing priority queues
    priority_queue<int, vector<int>,
                   greater<int> >
        Y;
    priority_queue<int, vector<int>,
                   greater<int> >
        Z;
 
    // Push array elements arr2[i] in Y
    for (int i = 0; i < N; i++) {
        Y.push(arr2[i]);
    }
 
    // Push array elements arr3[i] in Y
    for (int i = 0; i < N; i++) {
        Z.push(arr3[i]);
    }
 
    int x, y, z;
    int ans = 0;
 
    // Traverse the array arr1[]
    for (int i = 0; i < N; i++) {
 
        x = arr1[i];
        while (!Y.empty()
               && Y.top() <= x)
            Y.pop();
 
        // If Y is empty then there is
        // no more triplets possible
        if (Y.empty())
            break;
 
        y = Y.top();
        Y.pop();
 
        while (!Z.empty()
               && Z.top() <= y)
            Z.pop();
 
        // If Z is empty then there is
        // no more triplets possible
        if (Z.empty())
            break;
 
        z = Z.top();
        Z.pop();
 
        // Increment the triplets count
        ++ans;
    }
 
    // Return the maximum count of triplets
    return ans;
}
 
// Driver Code
int main()
{
    int X[] = { 9, 6, 14, 1, 8 };
    int Y[] = { 2, 10, 3, 12, 11 };
    int Z[] = { 15, 13, 5, 7, 4 };
    int N = sizeof(X) / sizeof(X[0]);
 
    cout << countTriplet(X, Y, Z, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
public class Main
{
    // Function to find the number of triplets
    // that are in increasing order from the
    // given three arrays after rearranging
    static int countTriplet(int[] arr1, int[] arr2,
                     int[] arr3, int N)
    {
       
        // Sort the given array arr[]
        Arrays.sort(arr1);
       
        // Initializing priority queues
        Vector<Integer> Y = new Vector<Integer>();
        Vector<Integer> Z = new Vector<Integer>();
       
        // Push array elements arr2[i] in Y
        for (int i = 0; i < N; i++) {
            Y.add(arr2[i]);
        }
        // Push array elements arr3[i] in Y
        for (int i = 0; i < N; i++) {
            Z.add(arr3[i]);
        }
        Collections.sort(Y);
        Collections.sort(Z);
       
        int x, y, z;
        int ans = 0;
       
        // Traverse the array arr1[]
        for (int i = 0; i < N; i++) {
       
            x = arr1[i];
            while (Y.size() > 0 && Y.get(0) <= x)
                Y.remove(0);
       
            // If Y is empty then there is
            // no more triplets possible
            if (Y.size() == 0)
                break;
       
            y = Y.get(0);
            Y.remove(0);
       
            while (Z.size() > 0 && Z.get(0) <= y)
                Z.remove(0);
       
            // If Z is empty then there is
            // no more triplets possible
            if (Z.size() == 0)
                break;
       
            z = Z.get(0);
            Z.remove(0);
       
            // Increment the triplets count
            ++ans;
        }
       
        // Return the maximum count of triplets
        return ans;
    }
     
    public static void main(String[] args) {
        int[] X = { 9, 6, 14, 1, 8 };
        int[] Y = { 2, 10, 3, 12, 11 };
        int[] Z = { 15, 13, 5, 7, 4 };
        int N = X.length;
       
        System.out.println(countTriplet(X, Y, Z, N));
    }
}
 
// This code is contributed by suresh07.


Python3




# Python program for the above approach
from queue import PriorityQueue
 
# Function to find the number of triplets
# that are in increasing order from the
# given three arrays after rearranging
def countTriplet(arr1, arr2, arr3, N):
 
    # Sort the given array arr[]
    arr1.sort();
 
    # Initializing priority queues
    Y = PriorityQueue();
    Z = PriorityQueue();
 
    # Push array elements arr2[i] in Y
    for i in range(N):
        Y.put(arr2[i]);
     
 
    # Push array elements arr3[i] in Y
    for i in range(N):
        Z.put(arr3[i]);
 
    x = 0
    y = 0
    z = 0
    ans = 0;
 
    # Traverse the array arr1[]
    for i in range(N):
 
        x = arr1[i];
        while (not Y.empty() and Y.queue[0] <= x):
            Y.get();
 
        # If Y is empty then there is
        # no more triplets possible
        if (Y.empty()):
            break;
 
        y = Y.queue[0];
        Y.get()
 
        while (not Z.empty() and Z.queue[0] <= y):
            Z.get();
 
        # If Z is empty then there is
        # no more triplets possible
        if (Z.empty()):
            break;
 
        z = Z.queue[0];
        Z.get();
 
        # Increment the triplets count
        ans += 1;
 
    # Return the maximum count of triplets
    return ans;
 
# Driver Code
X = [ 9, 6, 14, 1, 8 ];
Y = [ 2, 10, 3, 12, 11 ];
Z = [ 15, 13, 5, 7, 4 ];
N = len(X);
 
print(countTriplet(X, Y, Z, N));
 
# This code is contributed by _saurabh_jaiswal.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to find the number of triplets
    // that are in increasing order from the
    // given three arrays after rearranging
    static int countTriplet(int[] arr1, int[] arr2,
                     int[] arr3, int N)
    {
      
        // Sort the given array arr[]
        Array.Sort(arr1);
      
        // Initializing priority queues
        List<int> Y = new List<int>();
        List<int> Z = new List<int>();
      
        // Push array elements arr2[i] in Y
        for (int i = 0; i < N; i++) {
            Y.Add(arr2[i]);
        }
        // Push array elements arr3[i] in Y
        for (int i = 0; i < N; i++) {
            Z.Add(arr3[i]);
        }
        Y.Sort();
        Z.Sort();
      
        int x, y, z;
        int ans = 0;
      
        // Traverse the array arr1[]
        for (int i = 0; i < N; i++) {
      
            x = arr1[i];
            while (Y.Count > 0
                   && Y[0] <= x)
                Y.RemoveAt(0);
      
            // If Y is empty then there is
            // no more triplets possible
            if (Y.Count == 0)
                break;
      
            y = Y[0];
            Y.RemoveAt(0);
      
            while (Z.Count > 0
                   && Z[0] <= y)
                Z.RemoveAt(0);
      
            // If Z is empty then there is
            // no more triplets possible
            if (Z.Count == 0)
                break;
      
            z = Z[0];
            Z.RemoveAt(0);
      
            // Increment the triplets count
            ++ans;
        }
      
        // Return the maximum count of triplets
        return ans;
    }
 
  static void Main() {
    int[] X = { 9, 6, 14, 1, 8 };
    int[] Y = { 2, 10, 3, 12, 11 };
    int[] Z = { 15, 13, 5, 7, 4 };
    int N = X.Length;
  
    Console.Write(countTriplet(X, Y, Z, N));
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript




<script>
    // Javascript program for the above approach
     
    // Function to find the number of triplets
    // that are in increasing order from the
    // given three arrays after rearranging
    function countTriplet(arr1, arr2, arr3, N)
    {
       
        // Sort the given array arr[]
        arr1.sort(function(a, b){return a - b});
       
        // Initializing priority queues
        Y = [];
        Z = [];
       
        // Push array elements arr2[i] in Y
        for (let i = 0; i < N; i++) {
            Y.push(arr2[i]);
        }
        // Push array elements arr3[i] in Y
        for (let i = 0; i < N; i++) {
            Z.push(arr3[i]);
        }
        Y.sort(function(a, b){return a - b});
        Z.sort(function(a, b){return a - b});
       
        let x, y, z;
        let ans = 0;
       
        // Traverse the array arr1[]
        for (let i = 0; i < N; i++) {
       
            x = arr1[i];
            while (Y.length > 0 && Y[0] <= x)
                Y.shift();
       
            // If Y is empty then there is
            // no more triplets possible
            if (Y.Count == 0)
                break;
       
            y = Y[0];
            Y.shift();
       
            while (Z.length > 0 && Z[0] <= y)
                Z.shift();
       
            // If Z is empty then there is
            // no more triplets possible
            if (Z.length == 0)
                break;
       
            z = Z[0];
            Z.shift();
       
            // Increment the triplets count
            ++ans;
        }
       
        // Return the maximum count of triplets
        return ans;
    }
     
    X = [ 9, 6, 14, 1, 8 ];
    Y = [ 2, 10, 3, 12, 11 ];
    Z = [ 15, 13, 5, 7, 4 ];
    N = X.length;
   
    document.write(countTriplet(X, Y, Z, N));
     
    // This code is contributed by decode2207.
</script>


Output: 

3

 

Time Complexity: O(N * log N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads