{"type": "session", "harness": "cp-practice-sim", "id": "cp-0011", "name": "Matching Coin Pair", "mode": "chat", "topic": "Two pointers and sliding window", "difficulty": "beginner", "tags": ["two pointers", "sorted array"], "explanation_mode": "concise-rationale", "tool_scenario": "none", "expected_recovery": "N/A - no fault injected.", "languages": ["cpp", "rust", "kotlin"], "unexecuted_languages": [], "time_complexity": "O(n)", "space_complexity": "O(n)"} {"type": "message", "message": {"role": "system", "content": "You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. ", "timestamp": 1794039601700}} {"type": "message", "message": {"role": "user", "content": "# Matching Coin Pair\n\nTopic: Two pointers and sliding window | Difficulty: beginner\n\n## Problem\nA sorted row of n coins has values a[1] <= a[2] <= ... <= a[n]. Decide whether there are two different positions i < j with a[i] + a[j] = S.\n\n## Input\nThe first line contains n and S. The second line contains the n values in non-decreasing order.\n\n## Output\nPrint YES if such a pair exists, otherwise NO.\n\n## Constraints\n- 1 <= n <= 2*10^5\n- 1 <= a[i] <= 10^9\n- 1 <= S <= 2*10^9\n\n## Examples\n### Example 1\nInput:\n```\n5 9\n1 3 4 6 8\n```\nOutput:\n```\nYES\n```\nNote: 1 + 8 = 9.\n\n### Example 2\nInput:\n```\n3 4\n2 2 5\n```\nOutput:\n```\nYES\n```\nNote: Equal values at two different positions may be used together: 2 + 2 = 4.\n\nSolve this in C++17, Rust, Kotlin, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).", "timestamp": 1794039603537}} {"type": "message", "message": {"role": "assistant", "content": "**Approach.** Keep one pointer at each end. If the sum is too small move the left pointer right, if too large move the right pointer left.\n\n**Correctness.** When the sum is below S, the left element cannot pair with any element at or left of the right pointer, so it can be discarded; the symmetric argument holds for the right element.\n\n**Complexity.** Time: O(n). Space: O(n).\n\n### C++17\n```cpp\n#include \nusing namespace std;\nint main() {\n int n;\n long long s;\n scanf(\"%d %lld\", &n, &s);\n vector a(n);\n for (auto &x : a) scanf(\"%lld\", &x);\n int i = 0, j = n - 1;\n bool ok = false;\n while (i < j) {\n long long t = a[i] + a[j];\n if (t == s) { ok = true; break; }\n if (t < s) i++; else j--;\n }\n puts(ok ? \"YES\" : \"NO\");\n}\n```\n\n### Rust\n```rust\nuse std::io::*;\nfn main() {\n let mut s = String::new();\n stdin().read_to_string(&mut s).unwrap();\n let t: Vec = s.split_whitespace().map(|x| x.parse().unwrap()).collect();\n let n = t[0] as usize;\n let target = t[1];\n let a = &t[2..2 + n];\n let (mut i, mut j) = (0usize, n - 1);\n let mut ok = false;\n while i < j {\n let v = a[i] + a[j];\n if v == target {\n ok = true;\n break;\n }\n if v < target { i += 1; } else { j -= 1; }\n }\n println!(\"{}\", if ok { \"YES\" } else { \"NO\" });\n}\n```\n\n### Kotlin\n```kotlin\nimport java.util.*\n\nfun main() {\n val st = StringTokenizer(System.`in`.bufferedReader().readText())\n val n = st.nextToken().toInt()\n val s = st.nextToken().toLong()\n val a = LongArray(n) { st.nextToken().toLong() }\n var i = 0\n var j = n - 1\n var ok = false\n while (i < j) {\n val t = a[i] + a[j]\n if (t == s) { ok = true; break }\n if (t < s) i++ else j--\n }\n println(if (ok) \"YES\" else \"NO\")\n}\n```", "timestamp": 1794039605511}}