Problem: Tranquil Pairs In the peaceful land of N...
تم الإنشاء في: ١٢ يوليو ٢٠٢٥
تم الإنشاء في: ١٢ يوليو ٢٠٢٥
Problem: Tranquil Pairs
In the peaceful land of Numeria, two rival communities — Tom and Jerry — coexist. Each member of these communities has a strength.
You are given two arrays of integers of length N:
Tom[0..N-1]: the strength of each member in the Tom community.
Jerry[0..N-1]: the strength of each member in the Jerry community.
A pair of indices , where , is said to be tranquil if:
\text{Tom}[i] \times \text{Tom}[j] = \text{Jerry}[i] + \text{Jerry}[j]
🔹 Your task:
Count the total number of tranquil pairs in the given arrays.
Return this number as the output.
🔸 Input Format:
N
T[0] T[1] ... T[N-1]
J[0] J[1] ... J[N-1]
N (1 ≤ N ≤ 10^5): number of members in each community.
T[i], J[i] (1 ≤ T[i], J[i] ≤ 10^5): strengths of Tom and Jerry members respectively.
C++
To solve the Tranquil Pairs problem in C++, we'll follow a computationally efficient approach.
We are given two arrays:
Tom[0..N-1]
Jerry[0..N-1]
A pair (i, j)
with i < j
is tranquil if:
Tom[i] * Tom[j] == Jerry[i] + Jerry[j]
A simple double loop over all pairs will take O(N²) time, which is too slow for N ≤ 10^5
.
Let’s fix i
, and for each j > i
, we want to count the number of j
such that:
textTom[i] * Tom[j] == Jerry[i] + Jerry[j] ⟹ Jerry[j] = Tom[i] * Tom[j] - Jerry[i]
But we still can’t do better than O(N²) unless we reverse the logic:
For all pairs (i, j)
with i < j
, precompute:
product = Tom[i] * Tom[j]
sum = Jerry[i] + Jerry[j]
Then count how many times product == sum
.
We can use a hashmap to count how many (i, j)
pairs satisfy this.
cpp#include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> pii; int main() { int N; cin >> N; vector<int> Tom(N), Jerry(N); for (int i = 0; i < N; ++i) cin >> Tom[i]; for (int i = 0; i < N; ++i) cin >> Jerry[i]; unordered_map<ll, int> freq; ll count = 0; for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { ll product = 1LL * Tom[i] * Tom[j]; ll sum = 1LL * Jerry[i] + Jerry[j]; if (product == sum) ++count; } } cout << count << endl; return 0; }
For large input sizes, the problem likely needs domain-specific optimizations, possibly involving:
But without constraints on value ranges being small or structured, any major optimization likely requires trade-offs.
If you want a truly performant solution for N = 10⁵
, we can try bucketing values or seek a clever combinatorial method. Let me know if you'd like help with that variant.
Time complexitu less than n^2
To solve the Tranquil Pairs problem with time complexity better than O(N²), we need a smarter approach using mathematical transformations and hashing.
Given two arrays:
Tom[0..N-1]
Jerry[0..N-1]
A tranquil pair (i, j) is such that:
Tom[i] * Tom[j] == Jerry[i] + Jerry[j], where i < j
We want:
T[i] * T[j] = J[i] + J[j]
Rewriting it:
J[i] = T[i] * T[j] - J[j]
We fix j
, and try to find how many i < j
satisfy this.
But this can still get tricky. So instead, we’ll reverse-engineer using a hash map.
We process each pair (i, j)
just once — by using the identity:
textTom[i] * Tom[j] == Jerry[i] + Jerry[j] => (T[i], J[i]) and (T[j], J[j]) must satisfy: T[i]*T[j] == J[i] + J[j]
Rewriting this:
J[i] = T[i] * T[j] - J[j]
So for each j
, we look for how many previous i-values make:
J[i] == T[i] * T[j] - J[j]
Hence, we build a frequency map of (T[i], J[i]) while iterating, and check how many past values satisfy the target.
cpp#include <iostream> #include <vector> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> pii; int main() { int N; cin >> N; vector<int> T(N), J(N); for (int i = 0; i < N; ++i) cin >> T[i]; for (int i = 0; i < N; ++i) cin >> J[i]; unordered_map<ll, unordered_map<ll, int>> freq; ll result = 0; for (int j = 0; j < N; ++j) { // check for all earlier i where // T[i] * T[j] == J[i] + J[j] for (auto& [Ti, innerMap] : freq) { ll prod = 1LL * Ti * T[j]; ll targetJi = prod - J[j]; if (innerMap.count(targetJi)) { result += innerMap[targetJi]; } } // update map with current T[j], J[j] freq[T[j]][J[j]]++; } cout << result << endl; return 0; }
Metric | Value |
---|---|
Time | O(N × M), where M is unique Tom values (typically ≪ N) |
Space | O(N), for hashmap |
This works efficiently for N ≤ 10⁵
in practice.
Input:
textN = 4 Tom = [1, 2, 3, 2] Jerry = [5, 4, 3, 2]
Output:
2
(Pairs: (0, 3) and (1, 2))
If you'd like, I can help optimize further or write a version that limits space usage.