tinycodinglm / cp2 /cp-0011.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.82 kB
cp-practice-sim trace
System 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 com...

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.

User 2026-11-07T08:20:03.537Z

Matching Coin Pair

Topic: Two pointers and sliding window | Difficulty: beginner

Problem

A 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.

Input

The first line contains n and S. The second line contains the n values in non-decreasing order.

Output

Print YES if such a pair exists, otherwise NO.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= a[i] <= 10^9
  • 1 <= S <= 2*10^9

Examples

Example 1

Input:

markdown
5 9
1 3 4 6 8

Output:

markdown
YES

Note: 1 + 8 = 9.

Example 2

Input:

markdown
3 4
2 2 5

Output:

markdown
YES

Note: Equal values at two different positions may be used together: 2 + 2 = 4.

Solve this in C++17, Rust, Kotlin, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-07T08:20:05.511Z

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.

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.

Complexity. Time: O(n). Space: O(n).

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    long long s;
    scanf("%d %lld", &n, &s);
    vector<long long> a(n);
    for (auto &x : a) scanf("%lld", &x);
    int i = 0, j = n - 1;
    bool ok = false;
    while (i < j) {
        long long t = a[i] + a[j];
        if (t == s) { ok = true; break; }
        if (t < s) i++; else j--;
    }
    puts(ok ? "YES" : "NO");
}

Rust

rust
use std::io::*;
fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let t: Vec<i64> = s.split_whitespace().map(|x| x.parse().unwrap()).collect();
    let n = t[0] as usize;
    let target = t[1];
    let a = &t[2..2 + n];
    let (mut i, mut j) = (0usize, n - 1);
    let mut ok = false;
    while i < j {
        let v = a[i] + a[j];
        if v == target {
            ok = true;
            break;
        }
        if v < target { i += 1; } else { j -= 1; }
    }
    println!("{}", if ok { "YES" } else { "NO" });
}

Kotlin

kotlin
import java.util.*

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val s = st.nextToken().toLong()
    val a = LongArray(n) { st.nextToken().toLong() }
    var i = 0
    var j = n - 1
    var ok = false
    while (i < j) {
        val t = a[i] + a[j]
        if (t == s) { ok = true; break }
        if (t < s) i++ else j--
    }
    println(if (ok) "YES" else "NO")
}