Given two arrays X[] and Y[] of positive integers, find a number of pairs such that x^y > y^x where x is an element from X[] and y is an element from Y[].
Examples:
Input: X[] = {2, 1, 6}, Y = {1, 5}
Output: 3
Explanation: There are total 3 pairs where pow(x, y) is greater than pow(y, x) Pairs are (2, 1), (2, 5) and (6, 1)
Input: X[] = {10, 19, 18}, Y[] = {11, 15, 9}
Output: 2
Explanation: There are total 2 pairs where pow(x, y) is greater than pow(y, x) Pairs are (10, 11) and (10, 15)
C++
long long countPairsBruteForce( long long X[], long long Y[],
long long m, long long n)
{
long long ans = 0;
for ( int i = 0; i < m; i++)
for ( int j = 0; j < n; j++)
if ( pow (X[i], Y[j]) > pow (Y[j], X[i]))
ans++;
return ans;
}
|
Java
public static long countPairsBruteForce( long X[], long Y[],
int m, int n)
{
long ans = 0 ;
for ( int i = 0 ; i < m; i++)
for ( int j = 0 ; j < n; j++)
if (Math.pow(X[i], Y[j]) > Math.pow(Y[j], X[i]))
ans++;
return ans;
}
|
Python3
def countPairsBruteForce(X, Y, m, n):
ans = 0
for i in range (m):
for j in range (n):
if ( pow (X[i], Y[j]) > pow (Y[j], X[i])):
ans + = 1
return ans
|
C#
public static int countPairsBruteForce( int [] X, int [] Y,
int m, int n)
{
int ans = 0;
for ( int i = 0; i < m; i++)
for ( int j = 0; j < n; j++)
if (Math.Pow(X[i], Y[j]) > Math.Pow(Y[j], X[i]))
ans++;
return ans;
}
|
Javascript
function countPairsBruteForce(X, Y, m, n){
let ans = 0;
for (let i=0; i<m; i++ ){
for (let j=0;j<n;j++){
if ((Math.pow(X[i], Y[j]) > Math.pow(Y[j], X[i]))){
ans += 1;
}
}
}
return ans;
}
|
Time Complexity: O(M*N) where M and N are sizes of given arrays.
Auxiliary Space: O(1)
Efficient Solution:
The problem can be solved in O(nLogn + mLogn) time. The trick here is if y > x then x^y > y^x with some exceptions.
Following are simple steps based on this trick.
- Sort array Y[].
- For every x in X[], find the index idx of the smallest number greater than x (also called ceil of x) in Y[] using binary search, or we can use the inbuilt function upper_bound() in algorithm library.
- All the numbers after idx satisfy the relation so just add (n-idx) to the count.
Base Cases and Exceptions:
Following are exceptions for x from X[] and y from Y[]
- If x = 0, then the count of pairs for this x is 0.
- If x = 1, then the count of pairs for this x is equal to count of 0s in Y[].
- If x>1, then we also need to add count of 0s and count of 1s to the answer.
- x smaller than y means x^y is greater than y^x.
- x = 2, y = 3 or 4
- x = 3, y = 2
Note that the case where x = 4 and y = 2 is not there
Following diagram shows all exceptions in tabular form. The value 1 indicates that the corresponding (x, y) form a valid pair.

In the following implementation, we pre-process the Y array and count 0, 1, 2, 3 and 4 in it, so that we can handle all exceptions in constant time. The array NoOfY[] is used to store the counts.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count( int x, int Y[], int n, int NoOfY[])
{
if (x == 0)
return 0;
if (x == 1)
return NoOfY[0];
int * idx = upper_bound(Y, Y + n, x);
int ans = (Y + n) - idx;
ans += (NoOfY[0] + NoOfY[1]);
if (x == 2)
ans -= (NoOfY[3] + NoOfY[4]);
if (x == 3)
ans += NoOfY[2];
return ans;
}
int countPairs( int X[], int Y[], int m, int n)
{
int NoOfY[5] = { 0 };
for ( int i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
sort(Y, Y + n);
int total_pairs = 0;
for ( int i = 0; i < m; i++)
total_pairs += count(X[i], Y, n, NoOfY);
return total_pairs;
}
int main()
{
int X[] = { 2, 1, 6 };
int Y[] = { 1, 5 };
int m = sizeof (X) / sizeof (X[0]);
int n = sizeof (Y) / sizeof (Y[0]);
cout << "Total pairs = " << countPairs(X, Y, m, n);
return 0;
}
|
Java
import java.util.Arrays;
class Test {
static int count( int x, int Y[], int n, int NoOfY[])
{
if (x == 0 )
return 0 ;
if (x == 1 )
return NoOfY[ 0 ];
int idx = Arrays.binarySearch(Y, x);
int ans;
if (idx < 0 ) {
idx = Math.abs(idx + 1 );
ans = Y.length - idx;
}
else {
while (idx < n && Y[idx] == x) {
idx++;
}
ans = Y.length - idx;
}
ans += (NoOfY[ 0 ] + NoOfY[ 1 ]);
if (x == 2 )
ans -= (NoOfY[ 3 ] + NoOfY[ 4 ]);
if (x == 3 )
ans += NoOfY[ 2 ];
return ans;
}
static long countPairs( int X[], int Y[], int m, int n)
{
int NoOfY[] = new int [ 5 ];
for ( int i = 0 ; i < n; i++)
if (Y[i] < 5 )
NoOfY[Y[i]]++;
Arrays.sort(Y);
long total_pairs = 0 ;
for ( int i = 0 ; i < m; i++)
total_pairs += count(X[i], Y, n, NoOfY);
return total_pairs;
}
public static void main(String args[])
{
int X[] = { 2 , 1 , 6 };
int Y[] = { 1 , 5 };
System.out.println(
"Total pairs = "
+ countPairs(X, Y, X.length, Y.length));
}
}
|
Python3
import bisect
def count(x, Y, n, NoOfY):
if x = = 0 :
return 0
if x = = 1 :
return NoOfY[ 0 ]
idx = bisect.bisect_right(Y, x)
ans = n - idx
ans + = NoOfY[ 0 ] + NoOfY[ 1 ]
if x = = 2 :
ans - = NoOfY[ 3 ] + NoOfY[ 4 ]
if x = = 3 :
ans + = NoOfY[ 2 ]
return ans
def count_pairs(X, Y, m, n):
NoOfY = [ 0 ] * 5
for i in range (n):
if Y[i] < 5 :
NoOfY[Y[i]] + = 1
Y.sort()
total_pairs = 0
for x in X:
total_pairs + = count(x, Y, n, NoOfY)
return total_pairs
if __name__ = = '__main__' :
X = [ 2 , 1 , 6 ]
Y = [ 1 , 5 ]
print ( "Total pairs = " ,
count_pairs(X, Y, len (X), len (Y)))
|
C#
using System;
class GFG {
static int count( int x, int [] Y, int n, int [] NoOfY)
{
if (x == 0)
return 0;
if (x == 1)
return NoOfY[0];
int idx = Array.BinarySearch(Y, x);
int ans;
if (idx < 0) {
idx = Math.Abs(idx + 1);
ans = Y.Length - idx;
}
else {
while (idx < n && Y[idx] == x) {
idx++;
}
ans = Y.Length - idx;
}
ans += (NoOfY[0] + NoOfY[1]);
if (x == 2)
ans -= (NoOfY[3] + NoOfY[4]);
if (x == 3)
ans += NoOfY[2];
return ans;
}
static int countPairs( int [] X, int [] Y, int m, int n)
{
int [] NoOfY = new int [5];
for ( int i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
Array.Sort(Y);
int total_pairs = 0;
for ( int i = 0; i < m; i++)
total_pairs += count(X[i], Y, n, NoOfY);
return total_pairs;
}
public static void Main()
{
int [] X = { 2, 1, 6 };
int [] Y = { 1, 5 };
Console.Write(
"Total pairs = "
+ countPairs(X, Y, X.Length, Y.Length));
}
}
|
Javascript
<script>
function binarySearch(arr, x) {
let start=0, end=arr.length-1;
while (start<=end){
let mid=parseInt((start + end)/2);
if (arr[mid]===x) return mid;
else if (arr[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return -1;
}
function count(x , Y , n , NoOfY) {
if (x == 0)
return 0;
if (x == 1)
return NoOfY[0];
var idx = binarySearch(Y, x);
var ans;
if (idx < 0) {
idx = Math.abs(idx + 1);
ans = Y.length - idx;
} else {
while (idx < n && Y[idx] == x) {
idx++;
}
ans = Y.length - idx;
}
ans += (NoOfY[0] + NoOfY[1]);
if (x == 2)
ans -= (NoOfY[3] + NoOfY[4]);
if (x == 3)
ans += NoOfY[2];
return ans;
}
function countPairs(X , Y , m , n) {
var NoOfY = Array(5).fill(-1);
for ( var i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
Y.sort((a,b)=>a-b);
var total_pairs = 0;
for ( var i = 0; i < m; i++)
total_pairs += count(X[i], Y, n, NoOfY);
return total_pairs;
}
var X = [ 2, 1, 6 ];
var Y = [ 1, 5 ];
document.write( "Total pairs = " +
countPairs(X, Y, X.length, Y.length));
</script>
|
Time Complexity: O(n log n + m log n), where m and n are the sizes of arrays X[] and Y[] respectively. The sort step takes O(n log n) time. Then every element of X[] is searched in Y[] using binary search. This step takes O(m log n) time.
Auxiliary Space: O(1)
Approach#2: Divide and Conquer
this approach splits the input arrays into halves recursively until base cases are reached (arrays of length 0 or 1). At each recursive level, the function counts the number of valid pairs of elements between the two arrays by combining the counts of the subproblems.
Algorithm:
- Divide array X into two halves X_left and X_right.
- Divide array Y into two halves Y_left and Y_right.
- Recursively count the pairs in the four sub-arrays: (X_left, Y_left), (X_left, Y_right), (X_right, Y_left), (X_right, Y_right).
- Merge the counts from the four sub-arrays to get the total count of pairs.
C++
#include <iostream>
#include <vector>
using namespace std;
int countPairs(vector< int >& X, vector< int >& Y)
{
if (X.size() == 0 || Y.size() == 0) {
return 0;
}
if (X.size() == 1) {
int count = 0;
for ( int y : Y) {
if (y < X[0]) {
count++;
}
}
return count;
}
if (Y.size() == 1) {
int count = 0;
for ( int x : X) {
if (x > Y[0]) {
count++;
}
}
return count;
}
vector< int > X_left(X.begin(), X.begin() + X.size() / 2);
vector< int > X_right(X.begin() + X.size() / 2, X.end());
vector< int > Y_left(Y.begin(), Y.begin() + Y.size() / 2);
vector< int > Y_right(Y.begin() + Y.size() / 2, Y.end());
int count = 0;
count += countPairs(X_left, Y_left);
count += countPairs(X_left, Y_right);
count += countPairs(X_right, Y_left);
count += countPairs(X_right, Y_right);
return count;
}
int main()
{
vector< int > X = { 2, 1, 6 };
vector< int > Y = { 1, 5 };
cout << countPairs(X, Y) << endl;
return 0;
}
|
Java
import java.util.*;
public class CountPairs {
public static int countPairs( int [] X, int [] Y)
{
if (X.length == 0 || Y.length == 0 ) {
return 0 ;
}
if (X.length == 1 ) {
int count = 0 ;
for ( int y : Y) {
if (y < X[ 0 ]) {
count++;
}
}
return count;
}
if (Y.length == 1 ) {
int count = 0 ;
for ( int x : X) {
if (x > Y[ 0 ]) {
count++;
}
}
return count;
}
int [] X_left = Arrays.copyOfRange(X, 0 , X.length / 2 );
int [] X_right = Arrays.copyOfRange(X, X.length / 2 , X.length);
int [] Y_left = Arrays.copyOfRange(Y, 0 , Y.length / 2 );
int [] Y_right = Arrays.copyOfRange(Y, Y.length / 2 , Y.length);
int count = 0 ;
count += countPairs(X_left, Y_left);
count += countPairs(X_left, Y_right);
count += countPairs(X_right, Y_left);
count += countPairs(X_right, Y_right);
return count;
}
public static void main(String[] args)
{
int [] X = { 2 , 1 , 6 };
int [] Y = { 1 , 5 };
System.out.println(countPairs(X, Y));
}
}
|
Python3
def count_pairs(X, Y):
if len (X) = = 0 or len (Y) = = 0 :
return 0
if len (X) = = 1 :
return sum ( 1 for y in Y if y < X[ 0 ])
if len (Y) = = 1 :
return sum ( 1 for x in X if x > Y[ 0 ])
X_left, X_right = X[: len (X) / / 2 ], X[ len (X) / / 2 :]
Y_left, Y_right = Y[: len (Y) / / 2 ], Y[ len (Y) / / 2 :]
count = 0
count + = count_pairs(X_left, Y_left)
count + = count_pairs(X_left, Y_right)
count + = count_pairs(X_right, Y_left)
count + = count_pairs(X_right, Y_right)
return count
X = [ 2 , 1 , 6 ]
Y = [ 1 , 5 ]
print (count_pairs(X, Y))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountPairs(List< int > X, List< int > Y)
{
if (X.Count == 0 || Y.Count == 0)
{
return 0;
}
if (X.Count == 1)
{
int xCount = 0;
foreach ( int y in Y)
{
if (y < X[0])
{
xCount++;
}
}
return xCount;
}
if (Y.Count == 1)
{
int yCount = 0;
foreach ( int x in X)
{
if (x > Y[0])
{
yCount++;
}
}
return yCount;
}
List< int > X_left = X.GetRange(0, X.Count / 2);
List< int > X_right = X.GetRange(X.Count / 2, X.Count - X.Count / 2);
List< int > Y_left = Y.GetRange(0, Y.Count / 2);
List< int > Y_right = Y.GetRange(Y.Count / 2, Y.Count - Y.Count / 2);
int count = 0;
count += CountPairs(X_left, Y_left);
count += CountPairs(X_left, Y_right);
count += CountPairs(X_right, Y_left);
count += CountPairs(X_right, Y_right);
return count;
}
static void Main()
{
List< int > X = new List< int > { 2, 1, 6 };
List< int > Y = new List< int > { 1, 5 };
Console.WriteLine(CountPairs(X, Y));
}
}
|
Javascript
function count_pairs(X, Y) {
if (X.length == 0 || Y.length == 0) {
return 0;
}
if (X.length == 1) {
let count = 0;
for (let y of Y) {
if (y < X[0]) {
count++;
}
}
return count;
}
if (Y.length == 1) {
|
Time Complexity: O(n log n + m log m), where n is the length of array X and m is the length of array Y.
Auxiliary Space: O(n + m).
This article is contributed by Aarti_Rathi and Shubham Mittal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.