25. Number of Pairs
The problem can be found at the following link: Question Link
Note: Sorry for the delay in uploading this. My exams are currently ongoing, which has affected the timeline.
Problem Description
Given two positive integer arrays arr and brr, find the number of pairs such that x^y > y^x (raised to the power of) where x is an element from arr and y is an element from brr.
Example:
Input:
arr = [2, 1, 6]
brr = [1, 5]Output:
3Explanation: The pairs which follow x^y > y^x are: 2^1 > 1^2, 2^5 > 5^2, and 6^1 > 1^6.
My Approach
Counting Occurrences of
yValues Less Than 5:Create an array
NoOfYto count the number of occurrences of eachyvalue less than 5 inbrr.
Sorting Array
brr:Sort
brrto efficiently find the number of elements greater thanxusing binary search.
Iterating Through Array
arr:For each element
xinarr, calculate the number of valid pairs by:Adding the count of
yvalues wherex^y > y^x.Subtracting special cases where
xis 2 or 3.
Binary Search for Finding the First Greater Element:
Use binary search to find the first element in
brrgreater thanxand count how many such elements exist.
Time and Auxiliary Space Complexity
Expected Time Complexity: O((N + M)log(N)), as sorting
brrtakesO(N log N)time, and for each element inarr, we perform a binary search, which takesO(log N)time.Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables like
NoOfY.
Code (C++)
class Solution {
public:
int knapSack(int W, vector<int>& wt, vector<int>& val) {
int n = wt.size();
vector<int> K(W + 1, 0);
for (int i = 0; i < n; i++) {
for (int w = W; w >= wt[i]; w--) {
K[w] = max(K[w], val[i] + K[w - wt[i]]);
}
}
return K[W];
}
};Code (Java)
class Solution {
public long countPairs(int[] arr, int[] brr, int M, int N) {
int[] NoOfY = new int[5];
for (int i = 0; i < N; i++) {
if (brr[i] < 5) NoOfY[brr[i]]++;
}
Arrays.sort(brr);
long total_pairs = 0;
for (int x : arr) {
if (x == 0) continue;
if (x == 1) {
total_pairs += NoOfY[0];
continue;
}
int idx = Arrays.binarySearch(brr, x);
if (idx < 0) idx = -idx - 1;
else while (idx < N && brr[idx] == x) idx++;
long count = N - idx;
count += (NoOfY[0] + NoOfY[1]);
if (x == 2) count -= (NoOfY[3] + NoOfY[4]);
if (x == 3) count += NoOfY[2];
total_pairs += count;
}
return total_pairs;
}
}Code (Python)
class Solution:
def countPairs(self, arr, brr):
N = len(brr)
NoOfY = [0] * 5
for y in brr:
if y < 5:
NoOfY[y] += 1
brr.sort()
total_pairs = 0
for x in arr:
if x == 0:
continue
if x == 1:
total_pairs += NoOfY[0]
continue
idx = self.upper_bound(brr, x)
count = N - idx
count += (NoOfY[0] + NoOfY[1])
if x == 2:
count -= (NoOfY[3] + NoOfY[4])
if x == 3:
count += NoOfY[2]
total_pairs += count
return total_pairs
def upper_bound(self, arr, x):
lo, hi = 0, len(arr)
while lo < hi:
mid = (lo + hi) // 2
if arr[mid] <= x:
lo = mid + 1
else:
hi = mid
return loContribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated