Given two positive integer arrays a[] and b[], find the number of pairs such that xy > yx (raised to power of) where x is from a[] and y is from b[].
Examples:
Input: a[] = [2, 1, 6], b[] = [1, 5]
Output: 3
Explanation: The pairs which follow xy > yx are: 21 > 12, 25 > 52 and 61 > 16Input: a[] = [2 3 4 5], b[] = [1 2 3]
Output: 5
Explanation: The pairs which follow xy > yx are: 21 > 12 , 31 > 13 , 32 > 23 , 41 > 14 , 51 > 15.
[Naive Approach] Iterating over all pairs - O(n^2) Time and O(1) Space:
Iterate over all the possible pairs(x, y) in a[] and b[] and for each pair (x, y) check if x ^ y > y ^ x. If yes, then increment the count.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// Function to count pairs(x, y) where x ^ y > y ^ x
int countPairs(vector<int>& a, vector<int>& b)
{
int ans = 0;
// Iterate over all the possible pairs
for (int i = 0; i < a.size(); i++)
for (int j = 0; j < b.size(); j++)
// If x ^ y > y ^ x, increment ans
if (pow(a[i], b[j]) > pow(b[j], a[i]))
ans++;
return ans;
}
// Driver program
int main()
{
// Sample Input
vector<int> a = {2, 1, 6};
vector<int> b = {1, 5};
cout << countPairs(a, b);
return 0;
}
import java.util.*;
class GFG {
// Function to count pairs(x, y) where x ^ y > y ^ x
static int countPairs(int[] a, int[] b)
{
int ans = 0;
// Iterate over all the possible pairs
for (int i = 0; i < a.length; i++)
for (int j = 0; j < b.length; j++)
// If x ^ y > y ^ x, increment ans
if (Math.pow(a[i], b[j]) > Math.pow(b[j], a[i]))
ans++;
return ans;
}
// Driver program
public static void main(String[] args)
{
// Sample Input
int[] a = {2, 1, 6};
int[] b = {1, 5};
System.out.println(countPairs(a, b));
}
}
import math
# Function to count pairs(x, y) where x ^ y > y ^ x
def countPairs(a, b):
ans = 0
# Iterate over all the possible pairs
for i in range(len(a)):
for j in range(len(b)):
# If x ^ y > y ^ x, increment ans
if math.pow(a[i], b[j]) > math.pow(b[j], a[i]):
ans += 1
return ans
# Driver program
# Sample Input
a = [2, 1, 6]
b = [1, 5]
print(countPairs(a, b))
using System;
class GFG {
// Function to count pairs(x, y) where x ^ y > y ^ x
static int countPairs(int[] a, int[] b)
{
int ans = 0;
// Iterate over all the possible pairs
for (int i = 0; i < a.Length; i++)
for (int j = 0; j < b.Length; j++)
// If x ^ y > y ^ x, increment ans
if (Math.Pow(a[i], b[j]) > Math.Pow(b[j], a[i]))
ans++;
return ans;
}
// Driver program
public static void Main()
{
// Sample Input
int[] a = {2, 1, 6};
int[] b = {1, 5};
Console.WriteLine(countPairs(a, b));
}
}
/* Function to count pairs(x, y) where x ^ y > y ^ x */
function countPairs(a, b) {
let ans = 0;
// Iterate over all the possible pairs
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < b.length; j++) {
// If x ^ y > y ^ x, increment ans
if (Math.pow(a[i], b[j]) > Math.pow(b[j], a[i])) {
ans += 1;
}
}
}
return ans;
}
// Driver program
// Sample Input
let a = [2, 1, 6];
let b = [1, 5];
console.log(countPairs(a, b));
Output
3
[Expected Approach] Using Binary Search - O(n log n + m log n) Time and O(1) Space:Â
To optimize the solution, array b is first sorted. Sorting helps us use binary search to quickly find how many numbers in b are greater than a given value.
For most cases, if y > x, then x^y > y^x
So for each value aval in array a[], we mainly count elements in b[] that are greater than aval. However, some values do not follow this rule.
Special values are: 0, 1, 2, 3, and 4
Their frequencies are stored separately in array cnt for fast access.
Case 1: aval == 0: No valid pair exists because: 0^y < y^0, So answer is 0.
Case 2: aval == 1: Only pair with 0 is valid because: 1^0 > 0^1, So add count of 0.
Case 3: aval > 1:: Use binary search to count all elements in b[] greater than aval and add count of 0 and 1 because they always form valid pairs.
Special Cases: When aval = 2: Pairs with 3 and 4 are not valid because: 2^3 < 3^2, 2^4 = 4^2 So subtract count of 3 and 4.
When aval = 3 Pair (3,2) is valid because: 3^2 > 2^3 , So add count of 2.
Finally, add all valid counts obtained for every element of a[]. The final sum gives the total number of valid pairs.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// function to count valid pairs for one value of aval
int countPairsUtil(int aval, vector<int> &b, int n, vector<int> &countb)
{
// if aval is 0
if (aval == 0)
return 0;
// if aval is 1
if (aval == 1)
return countb[0];
// find first element greater than aval
auto idx = upper_bound(b.begin(), b.end(), aval);
// count elements greater than aval
int ans = b.end() - idx;
// add count of 0 and 1
ans += (countb[0] + countb[1]);
// special case for aval = 2
if (aval == 2)
ans -= (countb[3] + countb[4]);
// special case for aval = 3
if (aval == 3)
ans += countb[2];
return ans;
}
// function to count total valid pairs
int countPairs(vector<int> &a, vector<int> &b)
{
int m = a.size();
int n = b.size();
// store count of 0,1,2,3,4 in b
vector<int> countb(5, 0);
for (int i = 0; i < n; i++)
{
if (b[i] < 5)
countb[b[i]]++;
}
// sort b for binary search
sort(b.begin(), b.end());
int res = 0;
// count valid pairs for every value in a
for (int i = 0; i < m; i++)
{
res += countPairsUtil(a[i], b, n, countb);
}
return res;
}
int main()
{
vector<int> a = {2, 1, 6};
vector<int> b = {1, 5};
cout << countPairs(a, b);
return 0;
}
import java.util.*;
class GFG {
// function to count valid pairs for one value of aval
static int countPairsUtil(int aval, int[] b, int n, int[] countb)
{
// if aval is 0
if (aval == 0)
return 0;
// if aval is 1
if (aval == 1)
return countb[0];
// find first element greater than aval
int idx = upperBound(b, aval);
// count elements greater than aval
int ans = n - idx;
// add count of 0 and 1
ans += (countb[0] + countb[1]);
// special case for aval = 2
if (aval == 2)
ans -= (countb[3] + countb[4]);
// special case for aval = 3
if (aval == 3)
ans += countb[2];
return ans;
}
// function for upper bound
static int upperBound(int[] arr, int target)
{
int low = 0;
int high = arr.length;
while (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] <= target)
low = mid + 1;
else
high = mid;
}
return low;
}
// function to count total valid pairs
static int countPairs(int[] a, int[] b)
{
int m = a.length;
int n = b.length;
// store count of 0,1,2,3,4 in b
int[] countb = new int[5];
for (int i = 0; i < n; i++)
{
if (b[i] < 5)
countb[b[i]]++;
}
// sort b for binary search
Arrays.sort(b);
int res = 0;
// count valid pairs for every value in a
for (int i = 0; i < m; i++)
{
res += countPairsUtil(a[i], b, n, countb);
}
return res;
}
public static void main(String[] args)
{
int[] a = {2, 1, 6};
int[] b = {1, 5};
System.out.println(countPairs(a, b));
}
}
from bisect import bisect_right
# function to count valid pairs for one value of aval
def countPairsUtil(aval, b, n, countb):
# if aval is 0
if aval == 0:
return 0
# if aval is 1
if aval == 1:
return countb[0]
# find first element greater than aval
idx = bisect_right(b, aval)
# count elements greater than aval
ans = n - idx
# add count of 0 and 1
ans += (countb[0] + countb[1])
# special case for aval = 2
if aval == 2:
ans -= (countb[3] + countb[4])
# special case for aval = 3
if aval == 3:
ans += countb[2]
return ans
# function to count total valid pairs
def countPairs(a, b):
m = len(a)
n = len(b)
# store count of 0,1,2,3,4 in b
countb = [0] * 5
for i in range(n):
if b[i] < 5:
countb[b[i]] += 1
# sort b for binary search
b.sort()
res = 0
# count valid pairs for every value in a
for i in range(m):
res += countPairsUtil(a[i], b, n, countb)
return res
a = [2, 1, 6]
b = [1, 5]
print(countPairs(a, b))
using System;
class GFG {
// function to count valid pairs for one value of aval
static int countPairsUtil(int aval, int[] b, int n, int[] countb)
{
// if aval is 0
if (aval == 0)
return 0;
// if aval is 1
if (aval == 1)
return countb[0];
// find first element greater than aval
int idx = upperBound(b, aval);
// count elements greater than aval
int ans = n - idx;
// add count of 0 and 1
ans += (countb[0] + countb[1]);
// special case for aval = 2
if (aval == 2)
ans -= (countb[3] + countb[4]);
// special case for aval = 3
if (aval == 3)
ans += countb[2];
return ans;
}
// function for upper bound
static int upperBound(int[] arr, int target)
{
int low = 0;
int high = arr.Length;
while (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] <= target)
low = mid + 1;
else
high = mid;
}
return low;
}
// function to count total valid pairs
static int countPairs(int[] a, int[] b)
{
int m = a.Length;
int n = b.Length;
// store count of 0,1,2,3,4 in b
int[] countb = new int[5];
for (int i = 0; i < n; i++)
{
if (b[i] < 5)
countb[b[i]]++;
}
// sort b for binary search
Array.Sort(b);
int res = 0;
// count valid pairs for every value in a
for (int i = 0; i < m; i++)
{
res += countPairsUtil(a[i], b, n, countb);
}
return res;
}
static void Main()
{
int[] a = {2, 1, 6};
int[] b = {1, 5};
Console.WriteLine(countPairs(a, b));
}
}
using System;
class GFG {
// function to count valid pairs for one value of aval
static int countPairsUtil(int aval, int[] b, int n, int[] countb)
{
// if aval is 0
if (aval == 0)
return 0;
// if aval is 1
if (aval == 1)
return countb[0];
// find first element greater than aval
int idx = upperBound(b, aval);
// count elements greater than aval
int ans = n - idx;
// add count of 0 and 1
ans += (countb[0] + countb[1]);
// special case for aval = 2
if (aval == 2)
ans -= (countb[3] + countb[4]);
// special case for aval = 3
if (aval == 3)
ans += countb[2];
return ans;
}
// function for upper bound
static int upperBound(int[] arr, int target)
{
int low = 0;
int high = arr.Length;
while (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] <= target)
low = mid + 1;
else
high = mid;
}
return low;
}
// function to count total valid pairs
static int countPairs(int[] a, int[] b)
{
int m = a.Length;
int n = b.Length;
// store count of 0,1,2,3,4 in b
int[] countb = new int[5];
for (int i = 0; i < n; i++)
{
if (b[i] < 5)
countb[b[i]]++;
}
// sort b for binary search
Array.Sort(b);
int res = 0;
// count valid pairs for every value in a
for (int i = 0; i < m; i++)
{
res += countPairsUtil(a[i], b, n, countb);
}
return res;
}
static void Main()
{
int[] a = {2, 1, 6};
int[] b = {1, 5};
Console.WriteLine(countPairs(a, b));
}
}
Output
3