New 21 Game โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโ€”or scroll down to preview all options.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding New 21 Game
Detailed explanation and reference materials
Problem Overview

New 21 Game โ€“ Probability Explained with Sliding Window

Difficulty: Medium
Topics: Dynamic Programming, Probability, Sliding Window


๐Ÿง  Problem Understanding (In Simple Words)

Alice is playing a game where she keeps drawing points randomly.

Hereโ€™s how it works:

  • She starts with 0 points
  • Each time, she draws a number between 1 and maxPts
  • She keeps drawing until her total becomes at least k

๐Ÿ‘‰ Your goal: Find the probability that her final score is less than or equal to n


๐Ÿ’ก Intuition (How to Think About It)

This is not a typical problem โ€” itโ€™s about probability accumulation.

Think of it like:

๐Ÿ‘‰ At every step, Alice has multiple possible scores
๐Ÿ‘‰ Each score has a probability
๐Ÿ‘‰ We need to track how these probabilities evolve


๐ŸŽฏ Key Observation

  • Once Alice reaches k or more points, she stops
  • So final scores will always be in range:
    [k, k + maxPts - 1]
    

๐Ÿ‘‰ We only care about: Which of these final scores are โ‰ค n


๐Ÿš€ Approach (High-Level)

We use Dynamic Programming:

  1. Let dp[i] = probability of getting exactly i points
  2. Start with:
    dp[0] = 1 (100% probability)
    
  3. For each score:
    • Add probability from previous states
    • Maintain a sliding window to optimize calculation

โšก Optimization Insight (Sliding Window)

Instead of recalculating probabilities again and again:

๐Ÿ‘‰ Maintain a running sum of last maxPts values

This reduces time complexity significantly.


๐ŸŽฏ Visualization Using DrawToCode

This problem becomes much easier when visualized:

  • Each state represents a possible score
  • Arrows show transitions between scores
  • Probability flows from one state to another

๐Ÿ‘‰ Using DrawToCode, you can:

  • See how probability distributes across scores
  • Understand why certain ranges dominate
  • Debug DP transitions visually

๐Ÿ’ป Code (Java Example)

java
class Solution {
    public double new21Game(int n, int k, int maxPts) {
        if (k == 0 || n >= k + maxPts) return 1.0;

        double[] dp = new double[n + 1];
        dp[0] = 1.0;

        double windowSum = 1.0;
        double result = 0.0;

        for (int i = 1; i <= n; i++) {
            dp[i] = windowSum / maxPts;

            if (i < k) {
                windowSum += dp[i];
            } else {
                result += dp[i];
            }

            if (i - maxPts >= 0) {
                windowSum -= dp[i - maxPts];
            }
        }

        return result;
    }
}

โฑ๏ธ Complexity Analysis

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

๐Ÿ” Example Walkthrough

Input:

n = 6, k = 1, maxPts = 10

Explanation:

  • Alice draws only once (since k = 1)
  • Possible outcomes: 1 to 10
  • Favorable outcomes: 1 to 6 โ†’ 6 cases

๐Ÿ‘‰ Probability:

6 / 10 = 0.6

Input:

n = 21, k = 17, maxPts = 10

๐Ÿ‘‰ Alice keeps drawing until she reaches at least 17
๐Ÿ‘‰ Final scores range from 17 to 26

We calculate probability of scores โ‰ค 21 using DP

๐Ÿ‘‰ Output:

0.73278

๐Ÿงช Edge Cases to Consider

  • k = 0 โ†’ Alice never draws โ†’ probability = 1
  • n >= k + maxPts โ†’ all outcomes are valid โ†’ probability = 1
  • Very small ranges

๐Ÿ Final Thought

This problem teaches:

  • How to handle probabilities in DP
  • Sliding window optimization
  • Thinking in terms of state transitions

๐Ÿ‘‰ Once you visualize probability flow, this problem becomes much easier to understand

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
dp
maths
Sliding Window
java
Reference Link
https://leetcode.com/problems/new-21-game/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

๐Ÿ”’ Locked steps cannot be moved. Only unlocked steps are draggable.

๐Ÿ”Š Enable sound for swap feedback and completion effects.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.