-
Notifications
You must be signed in to change notification settings - Fork 7
/
116.cpp
77 lines (60 loc) · 1.8 KB
/
116.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// C++ program to finds the number of pairs (x, y)
// in an array such that x^y > y^x
#include <bits/stdc++.h>
using namespace std;
// Function to return count of pairs with x as one element
// of the pair. It mainly looks for all values in Y[] where
// x ^ Y[i] > Y[i] ^ x
int count(int x, int Y[], int n, int NoOfY[])
{
// If x is 0, then there cannot be any value in Y such
// that x^Y[i] > Y[i]^x
if (x == 0)
return 0;
// If x is 1, then the number of pais is equal to number
// of zeroes in Y[]
if (x == 1)
return NoOfY[0];
// Find number of elements in Y[] with values greater
// than x upper_bound() gets address of first greater
// element in Y[0..n-1]
int* idx = upper_bound(Y, Y + n, x);
int ans = (Y + n) - idx;
// If we have reached here, then x must be greater than
// 1, increase number of pairs for y=0 and y=1
ans += (NoOfY[0] + NoOfY[1]);
// Decrease number of pairs for x=2 and (y=4 or y=3)
if (x == 2)
ans -= (NoOfY[3] + NoOfY[4]);
// Increase number of pairs for x=3 and y=2
if (x == 3)
ans += NoOfY[2];
return ans;
}
// Function to return count of pairs (x, y) such that
// x belongs to X[], y belongs to Y[] and x^y > y^x
int countPairs(int X[], int Y[], int m, int n)
{
// To store counts of 0, 1, 2, 3 and 4 in array Y
int NoOfY[5] = { 0 };
for (int i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
// Sort Y[] so that we can do binary search in it
sort(Y, Y + n);
int total_pairs = 0; // Initialize result
// Take every element of X and count pairs with it
for (int i = 0; i < m; i++)
total_pairs += count(X[i], Y, n, NoOfY);
return total_pairs;
}
// Driver program
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;
}