Count of pairs from arrays A and B such that element in A is greater than element in B at that index
Last Updated :
25 Oct, 2022
Given two arrays A[] and B[] of size N, the task is to count the maximum number of pairs, where each pair contains one from each array, such that A[i] > B[i]. Also the array A can be rearranged any number of times.
Examples:
Input: A[] = {20, 30, 50}, B[]= {60, 40, 25}
Output: 2
Explanation:
Initially:
A[0] = 20 < B[0] = 60
A[1] = 30 < B[1] = 40
A[2] = 50 > B[2] = 25
Clearly, this arrangement has only 1 value such that A[i] > B[i].
This array A[] when rearranged to {20, 50, 30}:
A[0] = 20 < B[0] = 60
A[1] = 50 > B[1] = 40
A[2] = 30 > B[2] = 25
2 values follow the condition A[i] > B[i] which is the maximum for these set of arrays.
Input: A[] = {10, 3, 7, 5, 8}, B[] = {8, 6, 2, 5, 9}
Output: 4
Explanation:
Initially:
A[0] = 10 > B[0] = 8
A[1] = 3 < B[1] = 6
A[2] = 7 > B[2] = 2
A[3] = 5 = B[3] = 5
A[4] = 8 < B[4] = 9
Clearly, this arrangement has only 2 values such that A[i] > B[i].
This array A[] when rearranged to {10, 8, 5, 7, 3}:
A[0] = 10 > B[0] = 8
A[1] = 8 > B[1] = 6
A[2] = 5 > B[2] = 2
A[3] = 7 > B[3] = 5
A[4] = 3 < B[4] = 9
4 values follow the condition A[i] > B[i] which is the maximum for these set of arrays.
Approach: The idea is to use the concept of heap. Since the arrangement of B[] doesn't matter in the question, we can perform max heap on both the arrays. After performing max heap and storing the values in two different heaps, iterate through the heap corresponding to A[] and B[] to count the number of indices satisfying the given condition A[i] > B[i].
Below is the implementation of the above approach:
C++14
// C++ program to find the maximum count of
// values that follow the given condition
#include<bits/stdc++.h>
using namespace std;
// Function to find the maximum count of
// values that follow the given condition
int check(int A[], int B[], int N)
{
// Initializing the max-heap for the array A[]
priority_queue <int> pq1,pq2;
// Adding the values of A[] into max heap
for (int i = 0; i < N; i++) {
pq1.push(A[i]);
}
// Adding the values of B[] into max heap
for (int i = 0; i < N; i++) {
pq2.push(B[i]);
}
// Counter variable
int c = 0;
// Loop to iterate through the heap
for (int i = 0; i < N; i++) {
// Comparing the values at the top.
// If the value of heap A[] is greater,
// then counter is incremented
if (pq1.top()>pq2.top()) {
c++;
pq1.pop();
pq2.pop();
}
else {
if (pq2.size() == 0) {
break;
}
pq2.pop();
}
}
return (c);
}
// Driver code
int main()
{
int A[] = { 10, 3, 7, 5, 8 };
int B[] = { 8, 6, 2, 5, 9 };
int N = sizeof(A)/sizeof(A[0]);
cout<<(check(A, B, N));
}
// This code is contributed by mohit kumar 29
Java
// Java program to find the maximum count of
// values that follow the given condition
import java.util.*;
public class GFG {
// Function to find the maximum count of
// values that follow the given condition
static int check(int A[], int B[], int N)
{
// Initializing the max-heap for the array A[]
PriorityQueue<Integer> pq1
= new PriorityQueue<Integer>(
Collections.reverseOrder());
// Initializing the max-heap for the array B[]
PriorityQueue<Integer> pq2
= new PriorityQueue<Integer>(
Collections.reverseOrder());
// Adding the values of A[] into max heap
for (int i = 0; i < N; i++) {
pq1.add(A[i]);
}
// Adding the values of B[] into max heap
for (int i = 0; i < N; i++) {
pq2.add(B[i]);
}
// Counter variable
int c = 0;
// Loop to iterate through the heap
for (int i = 0; i < N; i++) {
// Comparing the values at the top.
// If the value of heap A[] is greater,
// then counter is incremented
if (pq1.peek().compareTo(pq2.peek()) == 1) {
c++;
pq1.poll();
pq2.poll();
}
else {
if (pq2.size() == 0) {
break;
}
pq2.poll();
}
}
return (c);
}
// Driver code
public static void main(String args[])
{
int A[] = { 10, 3, 7, 5, 8 };
int B[] = { 8, 6, 2, 5, 9 };
int N = A.length;
System.out.println(check(A, B, N));
}
}
Python3
# Python3 program to find the maximum count of
# values that follow the given condition
import heapq
# Function to find the maximum count of
# values that follow the given condition
def check(A, B,N):
# Initializing the max-heap for the array A[]
pq1 = []
pq2 = []
# Adding the values of A[] into max heap
for i in range(N):
heapq.heappush(pq1,-A[i])
# Adding the values of B[] into max heap
for i in range(N):
heapq.heappush(pq2,-B[i])
# Counter variable
c = 0
# Loop to iterate through the heap
for i in range(N):
# Comparing the values at the top.
# If the value of heap A[] is greater,
# then counter is incremented
if -pq1[0] > -pq2[0]:
c += 1
heapq.heappop(pq1)
heapq.heappop(pq2)
else:
if len(pq2) == 0:
break
heapq.heappop(pq2)
return (c)
# Driver code
A = [ 10, 3, 7, 5, 8 ]
B = [ 8, 6, 2, 5, 9 ]
N = len(A)
print(check(A, B, N))
# This code is contributed by apurva raj
C#
// C# program to find the maximum count of
// values that follow the given condition
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum count of
// values that follow the given condition
static int check(int[] A, int[] B, int N)
{
// Initializing the max-heap for the array A[]
List<int> pq1 = new List<int>();
// Initializing the max-heap for the array B[]
List<int> pq2 = new List<int>();
// Adding the values of A[] into max heap
for(int i = 0; i < N; i++)
{
pq1.Add(A[i]);
}
// Adding the values of B[] into max heap
for(int i = 0; i < N; i++)
{
pq2.Add(B[i]);
}
pq1.Sort();
pq1.Reverse();
pq2.Sort();
pq2.Reverse();
// Counter variable
int c = 0;
// Loop to iterate through the heap
for(int i = 0; i < N; i++)
{
// Comparing the values at the top.
// If the value of heap A[] is greater,
// then counter is incremented
if (pq1[0] > pq2[0])
{
c++;
pq1.RemoveAt(0);
pq2.RemoveAt(0);
}
else
{
if (pq2.Count == 0)
{
break;
}
pq2.RemoveAt(0);
}
}
return c;
}
// Driver code
static public void Main()
{
int[] A = { 10, 3, 7, 5, 8 };
int[] B = { 8, 6, 2, 5, 9 };
int N = A.Length;
Console.WriteLine(check(A, B, N));
}
}
// This code is contributed by avanitrachhadiya2155
JavaScript
<script>
// Javascript program to find the maximum count of
// values that follow the given condition
// Function to find the maximum count of
// values that follow the given condition
function check(A,B,N)
{
let pq1=[];
let pq2=[];
// Adding the values of A[] into max heap
for (let i = 0; i < N; i++) {
pq1.push(A[i]);
}
// Adding the values of B[] into max heap
for (let i = 0; i < N; i++) {
pq2.push(B[i]);
}
pq1.sort(function(a,b){return a-b;});
pq1.reverse();
pq2.sort(function(a,b){return a-b;});
pq2.reverse();
// Counter variable
let c = 0;
// Loop to iterate through the heap
for (let i = 0; i < N; i++) {
// Comparing the values at the top.
// If the value of heap A[] is greater,
// then counter is incremented
if (pq1[0] > pq2[0]) {
c++;
pq1.shift();
pq2.shift();
}
else {
if (pq2.length == 0) {
break;
}
pq2.shift();
}
}
return (c);
}
// Driver code
let A=[ 10, 3, 7, 5, 8];
let B=[8, 6, 2, 5, 9 ];
let N = A.length;
document.write(check(A, B, N));
// This code is contributed by patel2127
</script>
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Similar Reads
Count of distinct index pair (i, j) such that element sum of First Array is greater Given two arrays a[] and b[], both of size N. The task is to count the number of distinct pairs such that (a[i] + a[j] ) > ( b[i] + b[j] ) subject to condition that (j > i). Examples: Input: N = 5, a[] = {1, 2, 3, 4, 5}, b[] = {2, 5, 6, 1, 9} Output: 1 Explanation: Only one such pair exists an
13 min read
Count of distinct possible pairs such that the element from A is greater than the element from B Given two given arrays A and B of equal length, the task is to find the maximum number of distinct pairs of elements that can be chosen such that the element from A is strictly greater than the element from B. Examples: Input: A[]={20, 30, 50} , B[]={25, 60, 40} Output: 2 Explanation: (30, 25) and (
5 min read
Count elements in first Array with absolute difference greater than K with an element in second Array Given two arrays arr1[] and arr2[] and an integer K, our task is to find the number elements in the first array, for an element x, in arr1[], there exists at least one element y, in arr2[] such that absolute difference of x and y is greater than the integer K. Examples: Input: arr1 = {3, 1, 4}, arr2
7 min read
Count pairs in given Array having sum of index and value at that index equal Given an array arr[] containing positive integers, count the total number of pairs for which arr[i]+i = arr[j]+j such that 0â¤i<jâ¤n-1. Examples: Input: arr[] = { 6, 1, 4, 3 }Output: 3Explanation: The elements at index 0, 2, 3 has same value of a[i]+i as all sum to 6 {(6+0), (4+2), (3+3)}. Input: a
8 min read
Number of indices pair such that element pair sum from first Array is greater than second Array Given two integer arrays A[] and B[] of equal sizes, the task is to find the number of pairs of indices {i, j} in the arrays such that A[i] + A[j] > B[i] + B[j] and i < j.Examples: Input: A[] = {4, 8, 2, 6, 2}, B[] = {4, 5, 4, 1, 3} Output: 7 Explanation: There are a total of 7 pairs of indice
15+ min read