Maximize count of increasing triplets from any permutation of given 3 Arrays
Last Updated :
20 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int countTriplet( int arr1[], int arr2[],
int arr3[], int N)
{
sort(arr1, arr1 + N);
priority_queue< int , vector< int >,
greater< int > >
Y;
priority_queue< int , vector< int >,
greater< int > >
Z;
for ( int i = 0; i < N; i++) {
Y.push(arr2[i]);
}
for ( int i = 0; i < N; i++) {
Z.push(arr3[i]);
}
int x, y, z;
int ans = 0;
for ( int i = 0; i < N; i++) {
x = arr1[i];
while (!Y.empty()
&& Y.top() <= x)
Y.pop();
if (Y.empty())
break ;
y = Y.top();
Y.pop();
while (!Z.empty()
&& Z.top() <= y)
Z.pop();
if (Z.empty())
break ;
z = Z.top();
Z.pop();
++ans;
}
return ans;
}
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
import java.util.*;
public class Main
{
static int countTriplet( int [] arr1, int [] arr2,
int [] arr3, int N)
{
Arrays.sort(arr1);
Vector<Integer> Y = new Vector<Integer>();
Vector<Integer> Z = new Vector<Integer>();
for ( int i = 0 ; i < N; i++) {
Y.add(arr2[i]);
}
for ( int i = 0 ; i < N; i++) {
Z.add(arr3[i]);
}
Collections.sort(Y);
Collections.sort(Z);
int x, y, z;
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
x = arr1[i];
while (Y.size() > 0 && Y.get( 0 ) <= x)
Y.remove( 0 );
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.size() == 0 )
break ;
z = Z.get( 0 );
Z.remove( 0 );
++ans;
}
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));
}
}
|
Python3
from queue import PriorityQueue
def countTriplet(arr1, arr2, arr3, N):
arr1.sort();
Y = PriorityQueue();
Z = PriorityQueue();
for i in range (N):
Y.put(arr2[i]);
for i in range (N):
Z.put(arr3[i]);
x = 0
y = 0
z = 0
ans = 0 ;
for i in range (N):
x = arr1[i];
while ( not Y.empty() and Y.queue[ 0 ] < = x):
Y.get();
if (Y.empty()):
break ;
y = Y.queue[ 0 ];
Y.get()
while ( not Z.empty() and Z.queue[ 0 ] < = y):
Z.get();
if (Z.empty()):
break ;
z = Z.queue[ 0 ];
Z.get();
ans + = 1 ;
return ans;
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));
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int countTriplet( int [] arr1, int [] arr2,
int [] arr3, int N)
{
Array.Sort(arr1);
List< int > Y = new List< int >();
List< int > Z = new List< int >();
for ( int i = 0; i < N; i++) {
Y.Add(arr2[i]);
}
for ( int i = 0; i < N; i++) {
Z.Add(arr3[i]);
}
Y.Sort();
Z.Sort();
int x, y, z;
int ans = 0;
for ( int i = 0; i < N; i++) {
x = arr1[i];
while (Y.Count > 0
&& Y[0] <= x)
Y.RemoveAt(0);
if (Y.Count == 0)
break ;
y = Y[0];
Y.RemoveAt(0);
while (Z.Count > 0
&& Z[0] <= y)
Z.RemoveAt(0);
if (Z.Count == 0)
break ;
z = Z[0];
Z.RemoveAt(0);
++ans;
}
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));
}
}
|
Javascript
<script>
function countTriplet(arr1, arr2, arr3, N)
{
arr1.sort( function (a, b){ return a - b});
Y = [];
Z = [];
for (let i = 0; i < N; i++) {
Y.push(arr2[i]);
}
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;
for (let i = 0; i < N; i++) {
x = arr1[i];
while (Y.length > 0 && Y[0] <= x)
Y.shift();
if (Y.Count == 0)
break ;
y = Y[0];
Y.shift();
while (Z.length > 0 && Z[0] <= y)
Z.shift();
if (Z.length == 0)
break ;
z = Z[0];
Z.shift();
++ans;
}
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));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...