Find The Number Of Winning Players — 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 Find The Number Of Winning Players
Detailed explanation and reference materials
Problem Overview

Problem: Find the Number of Winning Players

Problem Statement:

We are given a 2D array pick where:

  • The first column represents the ball number.
  • The second column represents the color of the ball.

The task is to return the count of balls that satisfy the following condition:

  • For a particular ball number in the first column, it should be less than the count of balls with the same color as indicated by the second column.

Approach:

Step 1: Sorting the Array

  • Sort by Ball Number (First Column):
    First, sort the array based on the first column (ball number) in ascending order.

  • Sort by Ball Color (Second Column):
    Without altering the first column, sort the array again based on the second column (ball color) in ascending order.

Step 2: Initial Setup

  • Base Condition for Ball Number 0:
    • If ball number 0 is present in the array, it automatically satisfies the condition. In this case, increment the count by one.
    • Set prev1 to 0 (representing the ball number) and prev2 to the color of the ball in the last occurrence of ball number 0.
    • Increment the pointer to point to the ball number next to 0.

Step 3: Iterative Process

  • Pointer Initialization:

    • If the base condition is not met, point to the second row with a pointer ti, and assign the current count of balls with the same color as the ti-th ball number to ccount.
    • Set prev1 and prev2 to the first and second column values of the 0th row, respectively.
  • Loop through the Array:

    • Iterate through the array using the ti pointer.

    • Skipping Unnecessary Iterations:

      • If pick[i][0] - ccount > (pick.size() - i) - 1, skip the rest of the loop. This condition checks if the current ball number is greater than the number of remaining rows in the array.

Step 4: Updating the Count

  • Check Matching Condition:

    • If the current ball number (pick[i][0]) and ball color (pick[i][1]) match prev1 and prev2, respectively, increment the current count (ccount).
  • Final Condition:

    • If ccount == pick[i][1] + 1, increment the final count.

Step 5: Reset Counters

  • If the current ball number or color does not match prev1 or prev2, update prev1 to the current ball number, prev2 to the current ball color, and reset ccount to 1.

Step 6: Final Step

  • After iterating through the entire array, return the final count of balls that satisfy the condition.

Example Walkthrough

Example 1:

Input:

plaintext
pick = [
  [0, 3],
  [1, 3],
  [2, 4],
  [3, 2],
  [4, 2]
]

```Output:

1```
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
tries
java
Reference Link
https://leetcode.com/problems/find-the-number-of-winning-players/

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.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.