tinycodinglm / cp2 /cp-0032.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
9.76 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. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

User 2026-11-08T05:20:03.537Z

Matching Coupons

Topic: Hashing and counting (maps/sets) | Difficulty: easy

Problem

A shop has n coupons, coupon i has face value a_i. Two coupons can be stapled together if their values add up to exactly k. Count the pairs of positions (i, j) with i < j and a_i + a_j = k.

Input

The first line contains n and k. The second line contains n integers a_1 ... a_n.

Output

Print one integer: the number of pairs.

Constraints

  • 1 <= n <= 2*10^5
  • |a_i| <= 10^9, |k| <= 2*10^9

Examples

Example 1

Input:

markdown
5 6
1 5 5 1 5

Output:

markdown
6

Note: Each of the two 1s pairs with each of the three 5s.

Example 2

Input:

markdown
4 8
4 4 4 4

Output:

markdown
6

Note: When a value is its own partner, every two positions form a pair: C(4,2) = 6.

Solve this task. Write solutions in Java 17, Go, Ruby, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-08T05:20:05.511Z

Writing the Java 17 solution and testing it on the examples.

Assistant 2026-11-08T05:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-08T05:20:16.536Z

Compiling the Java 17 solution.

Assistant 2026-11-08T05:20:20.465Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T05:20:27.386Z

Cross-checking with the Go version.

Assistant 2026-11-08T05:20:31.785Z

Compiling the Go solution.

Assistant 2026-11-08T05:20:35.832Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T05:20:42.930Z

Cross-checking with the Ruby version.

Assistant 2026-11-08T05:20:46.547Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T05:20:53.000Z

Approach. Scan left to right, keeping a map of how many times each value has been seen. For the current value x add count[k - x] to the answer, then record x.

Correctness. Every pair (i, j) with i < j is counted exactly once, when j is processed and the earlier partner is already in the map.

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

All solutions below passed every example.

Java 17

java
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
        st.nextToken();
        int n = (int) st.nval;
        st.nextToken();
        long k = (long) st.nval;
        HashMap<Long, Integer> cnt = new HashMap<>();
        long ans = 0;
        for (int i = 0; i < n; i++) {
            st.nextToken();
            long x = (long) st.nval;
            ans += cnt.getOrDefault(k - x, 0);
            cnt.merge(x, 1, Integer::sum);
        }
        System.out.println(ans);
    }
}

Go

go
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    in := bufio.NewReaderSize(os.Stdin, 1<<20)
    var n int
    var k int64
    fmt.Fscan(in, &n, &k)
    cnt := make(map[int64]int64)
    var ans int64
    for i := 0; i < n; i++ {
        var x int64
        fmt.Fscan(in, &x)
        ans += cnt[k-x]
        cnt[x]++
    }
    fmt.Println(ans)
}

Ruby

ruby
n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
cnt = Hash.new(0)
ans = 0
a.each do |x|
  ans += cnt[k - x]
  cnt[x] += 1
end
puts ans