412. — Sislovesme
Both limits satisfy the given constraints ( ∑ N ≤ 10⁶ ). Below are clean, production‑ready solutions in C++ (17) and Python 3 . Both follow the algorithm described above and use fast I/O to handle the maximum input size. C++ (GNU‑C++17) #include <bits/stdc++.h> using namespace std;
From Lemma 1 every increment corresponds to a genuine mutual‑love pair. From Lemma 2 every genuine pair contributes exactly one increment. From Lemma 3 no non‑mutual pair contributes any increment. Therefore the total number of increments equals precisely the number of mutual‑love pairs. ∎ 5️⃣ Complexity analysis Time – The loop visits each of the N people once, performing O(1) work per iteration: O(N) per test case. 412. Sislovesme
2 4 2 1 4 3 5 2 3 1 5 4
Because a, b is a mutual‑love pair, we have love[a] = b and love[b] = a . Assume without loss of generality that a < b . Both limits satisfy the given constraints ( ∑
Memory – The array love[1…N] is stored: . C++ (GNU‑C++17) #include <bits/stdc++
When the loop later reaches i = b , the first condition fails ( b < a is false), so the pair is counted again. ∎ Lemma 3 If a pair i, j is not a mutual‑love pair, the algorithm never increments mutualPairs for it.