Rotten Oranges Algorithm
The Rotten Oranges problem is a grid-based problem that involves determining the minimum time required for all fresh oranges to rot given an initial configuration of fresh and rotten oranges.
Problem Definition
Given: A 2D grid where each cell can have one of three values: 0: an empty cell 1: a fresh orange 2: a rotten orange
Video Explanation

Objective: Return the minimum number of minutes needed for all fresh oranges to become rotten. If all oranges can’t rot, return -1. Algorithm Overview
Breadth-First Search (BFS) Approach:
Use BFS to simulate the spread of rotting from each rotten orange to adjacent fresh oranges. Each level of BFS represents one minute. Initialization:
Initialize a queue with all initial rotten oranges and count the fresh oranges. Track the minutes taken for all oranges to rot. Processing BFS Levels:
For each rotten orange, attempt to rot adjacent fresh oranges (up, down, left, right). Add newly rotten oranges to the queue and decrease the count of fresh oranges. Result Evaluation:
If there are no remaining fresh oranges after BFS, return the minutes taken. If fresh oranges remain, return -1. Time Complexity Time Complexity: O(n * m), where n is the number of rows and m is the number of columns, as each cell is processed at most once. Space Complexity: O(n * m) for the BFS queue. C++ Implementation