Easy & Detailed Solution✅ |TC: O(1) 🔥 | Step - By - Step Explain 🚀 | C++ | Java | Py | Js  
🧠 IntuitionThe problem requires us to determine the maximum number of containers that can be placed on an n x n desk while ensuring that their total weight does
# 🧠 Intuition  
The problem requires us to determine the maximum number of containers that can be placed on an `n x n` desk while ensuring that their total weight does not exceed `maxWeight`.  

- The **total number of containers** that can fit on the desk is `n * n`.  
- Each container has a weight of `w`.  
- The goal is to find the maximum number of containers such that their total weight stays within `maxWeight`.  

---

# 🚀 Approach  
1. **Calculate the total possible containers**:  
   - Since the desk is a square of size `n x n`, it can hold `desk = n * n` containers.  

2. **Check weight constraints**:  
   - If all `desk` containers can fit within the given weight limit (`desk * w <= maxWeight`), return `desk`.  
   - Otherwise, decrement the number of containers one by one until the condition holds.  

3. **Optimized Calculation**:  
   - Instead of iterating, we can directly compute the answer as:  

     ```cpp
     int maxContainers = min(desk, maxWeight / w);
     ```
     
   - This gives us the maximum valid number of containers in **constant time** instead of using a loop.  

---

# ⏳ Complexity Analysis  

- **Time Complexity**:  
  - *Brute Force*: \(O(n^2)\) (if we check each possible count)  
  - **Optimized Approach**: \(O(1)\) ✅ (since we use direct computation)  

- **Space Complexity**:  
  - \(O(1)\) ✅ (Only a few integer variables are used)  

---

## 📌 Step-by-Step Execution  
Example: `n = 3, w = 4, maxWeight = 20`  

| Step | Desk | desk * w | Condition (≤ maxWeight) | Action |
|------|------|----------|------------------------|--------|
| 1    | 9    | 36       | No ❌                  | Reduce desk → 8 |
| 2    | 8    | 32       | No ❌                  | Reduce desk → 7 |
| 3    | 7    | 28       | No ❌                  | Reduce desk → 6 |
| 4    | 6    | 24       | No ❌                  | Reduce desk → 5 |
| 5    | 5    | 20       | Yes ✅                 | Store ans = 5, exit loop |

---

# 💻 Code  



```javascript []
class Solution {
    maxContainers(n, w, maxWeight) {
        // Calculate the total number of containers that can be placed on the desk
        let desk = n * n;
        let ans = 0; // Variable to store the maximum number of containers that can be supported

        // Iterate from the maximum possible number of containers down to 0
        for (let i = desk; i >= 0; i--) {
            // Check if the total weight of 'desk' containers is within the maximum weight limit
            if (desk * w <= maxWeight) {
                ans = desk; // If valid, store the answer
                break; // Exit the loop as we've found the maximum possible containers
            } else {
                desk--; // If weight exceeds maxWeight, reduce the number of containers and check again
            }
        }

        return ans; // Return the maximum number of containers that can be supported
    }
}

```
```python []
class Solution:
    def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
        # Calculate the total number of containers that can be placed on the desk
        desk = n * n  
        ans = 0  # Variable to store the maximum number of containers that can be supported

        # Iterate from the maximum possible number of containers down to 0
        for i in range(desk, -1, -1):
            # Check if the total weight of 'desk' containers is within the maximum weight limit
            if desk * w <= maxWeight:
                ans = desk  # If valid, store the answer
                break  # Exit the loop as we've found the maximum possible containers
            else:
                desk -= 1  # If weight exceeds maxWeight, reduce the number of containers and check again

        return ans  # Return the maximum number of containers that can be supported

```
```java []
class Solution {
    public int maxContainers(int n, int w, int maxWeight) {
        // Calculate the total number of containers that can be placed on the desk
        int desk = n * n;  
        int ans = 0; // Variable to store the maximum number of containers that can be supported

        // Iterate from the maximum possible number of containers down to 0
        for (int i = desk; i >= 0; i--) {
            // Check if the total weight of 'desk' containers is within the maximum weight limit
            if (desk * w <= maxWeight) {
                ans = desk; // If valid, store the answer
                break; // Exit the loop as we've found the maximum possible containers
            } else {
                desk--; // If weight exceeds maxWeight, reduce the number of containers and check again
            }
        }
        
        return ans; // Return the maximum number of containers that can be supported
    }
}
```
```C++ []
class Solution {
public:
    int maxContainers(int n, int w, int maxWeight) 
    {
        // Calculate the total number of containers that can be placed on the desk
        int desk = n * n;  
        int ans = 0;  

        // Iterate from the maximum possible number of containers down to 0
        for (int i = desk; i >= 0; i--) 
        {
            // Check if the total weight of 'desk' containers is within the max weight limit
            if (desk * w <= maxWeight) 
            {
                ans = desk;  
                break;      
            } 
            else 
            {
                desk--;  
            }
        }

        return ans;  
    }
};
```


['C++', 'Java', 'Python3', 'JavaScript']