Android & iPhone apps are coming soon. Log in for a plan and reminders made for you โ†’

Day 72MediumLeetCode (4103)

Remove Elements by Type and Color

Run Compression + Stack

  1. Basics
  2. 2Paper
  3. 3Code it
  4. 5Visualize
  5. 6Accepted
  6. 7Review

Go step by step: try it yourself before the video and the visual. Progress saves on this device.

0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15

Step 1

Know these before you start

Practise each one here, or tick what you already know

Concept video

Watch the concept video if the pattern is new to you.

Step 2

Try it on paper

5โ€“10 minutes, no code yet

ProblemProblem statementYou are given three integer arrays types, colors, and groupSize, where types and colors have length n, and groupSize has length m.

Problem Statement

You are given three integer arrays types, colors, and groupSize, where types and colors have length n, and groupSize has length m.

  • types[i] is the type of the element at index i.
  • colors[i] is the color of the element at index i.
  • groupSize[t] is the required number of elements in a removable group of type t.

In one operation, you may remove exactly groupSize[t] consecutive elements if all of them have type t and the same color. After each removal, the remaining elements become adjacent again while maintaining their relative order.

Return true if it is possible to remove all elements using a sequence of valid operations. Otherwise, return false.

Added 2026-11-08 from Srujan's LeetCode Recent AC list (photo) โ€” not previously in the repo.


Input / Output

Input:  types = [0,1,1,0], colors = [5,3,3,5], groupSize = [2,2]
Output: true
Explanation: Remove indices 1-2 (type 1, color 3), the two 0s become
adjacent, then remove them too.

Input:  types = [0,1,0], colors = [9,9,9], groupSize = [3,2]
Output: false
Explanation: No consecutive group satisfies its required size.

Input:  types = [2,0,1,2], colors = [7,5,4,7], groupSize = [3,1,2]
Output: false
Explanation: Removing index 2 (type 1, groupSize 1) leaves [2,0,2],
and nothing else can be removed.

Code it on LeetCode

There's no editor for this one here yet, so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.

Solve on LeetCode

An error or a Wrong Answer there? What each one means.

Step 6

Get it accepted

All test cases pass

Step 7

Review it so it sticks

Re-solve from memory after

The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.

Your notes

Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.

Solve it yourself before you look

Reading the solution first feels like progress, but it makes the next similar problem โ€” and the interview version โ€” much harder, because you skipped the part where you figure it out. Give it an honest 20โ€“30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.

Hidden: approach ยท solution code

Stuck? One hint at a time.

    or find it yourself with the pattern finder

    Revising this one?

    • โ†’Solve it from memory before peeking
    • โ†’Say the pattern out loud, not just the code
    • โ†’Explain it like you would to a friend
    • โ†’Try it again in a different language