← CSES Better

Count Good Subsequences of a Sorted Array

ZIO · 2026 · Zonal Informatics Olympiad · P2

Open official problem ↗
Ask AI

Open a hint-first prompt in a new AI chat. Grok may ask you to confirm before sending.

Open Claude Desktop

subsequences · dynamic-programming · counting

The organizer’s problem statement is available at the official source link.

subsequence dag

Keep each adjacent value jump within K

A subsequence is good when each chosen value differs from the previous chosen value by at most K. Count index subsequences, including distinct choices with equal values.

i<j; <= Kj<k; <= Kindex iindex jindex k
  1. 1

    Create position nodes

    Treat array positions as separate nodes, including equal-valued positions.
  2. 2

    Connect valid jumps

    Connect compatible later positions when their value gap is at most K.
  3. 3

    Count index paths

    Count nonempty paths ending at every node.
Tools
1
2
3
4
5
6
7
8
9
10
11
12
Draft saved locally
|12 lines, 156 chars

Run output

Run your code to see output.