-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNumber of pairs.cpp
More file actions
45 lines (37 loc) · 1.03 KB
/
Number of pairs.cpp
File metadata and controls
45 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Given two arrays X and Y of positive integers, find the number of pairs such that xy > yx (raised to power of) where x is an element from X and y is an element from Y.
Example 1:
Input:
M = 3, X[] = [2 1 6]
N = 2, Y[] = [1 5]
Output: 3
Explanation:
The pairs which follow xy > yx are
as such: 21 > 12, 25 > 52 and 61 > 16 .
Example 2:
Input:
M = 4, X[] = [2 3 4 5]
N = 3, Y[] = [1 2 3]
Output: 5
Explanation:
The pairs for the given input are
21 > 12 , 31 > 13 , 32 > 23 , 41 > 14 ,
51 > 15 .
Your Task:
This is a function problem. You only need to complete the function countPairs() that takes X, Y, M, N as parameters and returns the total number of pairs.
Expected Time Complexity: O((N + M)log(N)).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ M, N ≤ 105
1 ≤ X[i], Y[i] ≤ 103
*/
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;
}