
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find number of pairs (x, y) in an array such that x^y > y^x in C++
Suppose we have two arrays X and Y of positive integers. Find the number of pairs such that x^y > y^x, where x is an element of X and y is an element of Y. Suppose the X = [2, 1, 6], and Y = [1, 5], then output will be 3. As there are three pairs, these are (2, 1), (2, 5) and (6, 1)
We can solve this in an efficient way. The logic is simple, it will be when y > x then x^y > y^x with some exceptions. So this is the trick.
Sort the array Y
for each element x in X, we have to find the index of the smallest number that is greater than x in Y. We will use the binary search to do so. Otherwise we can use the upper_bound() function also.
All the number after the found index satisfy the relation so just add that into the count.
Example
#include <iostream> #include <algorithm> using namespace std; int count(int x, int Y[], int n, int no_of_y[]) { if (x == 0) return 0; if (x == 1) return no_of_y[0]; int* index = upper_bound(Y, Y + n, x); int ans = (Y + n) - index; ans += (no_of_y[0] + no_of_y[1]); if (x == 2) ans -= (no_of_y[3] + no_of_y[4]); if (x == 3) ans += no_of_y[2]; return ans; } int howManyPairs(int X[], int Y[], int m, int n) { int no_of_y[5] = {0}; for (int i = 0; i < n; i++) if (Y[i] < 5) no_of_y[Y[i]]++; sort(Y, Y + n); int total_pairs = 0; for (int i=0; i< m; i++) total_pairs += count(X[i], Y, n, no_of_y); 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 pair count: " << howManyPairs(X, Y, m, n); }
Output
Total pair count: 3
- Related Articles
- Count of pairs (x, y) in an array such that x < y in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Solve the following pairs of equations:\( \frac{2 x y}{x+y}=\frac{3}{2} \)\( \frac{x y}{2 x-y}=\frac{-3}{10}, x+y ≠ 0,2 x-y ≠ 0 \)
- Find larger of x^y and y^x in C++
- Verify : (i) \( x^{3}+y^{3}=(x+y)\left(x^{2}-x y+y^{2}\right) \)(ii) \( x^{3}-y^{3}=(x-y)\left(x^{2}+x y+y^{2}\right) \)
- Factorize:$4(x - y)^2 - 12(x -y) (x + y) + 9(x + y)^2$
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- Factorize:\( x^{2}+y-x y-x \)
- Find the sum of the following arithmetic progressions:\( \frac{x-y}{x+y}, \frac{3 x-2 y}{x+y}, \frac{5 x-3 y}{x+y}, \ldots \) to \( n \) terms
- Find the smallest number X such that X! contains at least Y trailing zeros in C++
- Solve the following pairs of equations:\( x+y=3.3 \)\( \frac{0.6}{3 x-2 y}=-1,3 x-2 y ≠ 0 \)
- Subtract $24 x y-10 y-18 x$ from $30 x y +12 y+14 x$
- Factorize:\( x\left(x^{3}-y^{3}\right)+3 x y(x-y) \)
- If \( 3^{x}=5^{y}=(75)^{z} \), show that \( z=\frac{x y}{2 x+y} \).

Advertisements