You are given an integer n. Consider an equilateral triangle of side length n, broken up into n² unit equilateral triangles.
The triangle has n 1-indexed rows where the i-th row has 2i - 1 unit equilateral triangles. The triangles in the i-th row are also 1-indexed with coordinates from (i, 1) to (i, 2i - 1).
Two triangles are neighbors if they share a side. For example:
Triangles (1,1) and (2,2) are neighbors
Triangles (3,2) and (3,3) are neighbors
Triangles (2,2) and (3,3) are not neighbors because they do not share any side
Initially, all unit triangles are white. You want to choose k triangles and color them red. We will then run the following algorithm:
Choose a white triangle that has at least two red neighbors
If there is no such triangle, stop the algorithm
Color that triangle red
Go to step 1
Choose the minimum k possible and set k triangles red before running this algorithm such that after the algorithm stops, all unit triangles are colored red.
Return a 2D list of the coordinates of the triangles that you will color red initially. The answer has to be of the smallest size possible. If there are multiple valid solutions, return any.
Input & Output
Example 1 — Small Triangle (n=2)
$Input:n = 2
›Output:[[1,1],[2,2]]
💡 Note:For n=2, we have triangles (1,1), (2,1), (2,2), (2,3). Starting with (1,1) and (2,2) red, the algorithm can color (2,1) and (2,3) since they each have 2 red neighbors.
Example 2 — Medium Triangle (n=3)
$Input:n = 3
›Output:[[1,1],[3,1],[3,5]]
💡 Note:For n=3, we place red triangles at the top corner and two bottom corners. This strategic placement allows the spreading algorithm to color all remaining triangles through the neighbor rule.
Example 3 — Single Triangle (n=1)
$Input:n = 1
›Output:[[1,1]]
💡 Note:For n=1, there's only one triangle (1,1), so we must color it initially. No spreading is needed.
The key insight is that this is a domination problem on a triangular grid. The optimal solution uses corner positions plus strategic central positions to minimize the initial set while ensuring the spreading algorithm can reach all triangles. Best approach combines geometric analysis with known optimal patterns. Time: O(n⁴), Space: O(n²)
Common Approaches
✓
Mathematical Analysis
⏱️ Time: O(n⁴)
Space: O(n²)
Analyze the triangle structure to identify key positions that maximize spreading potential. Focus on corner positions and central nodes that have the most influence.
Try All Combinations
⏱️ Time: O(2^(n²))
Space: O(n²)
Generate all possible combinations of triangles starting from k=1, test if each combination leads to full coloring through the spreading algorithm, return the first valid combination found.
Mathematical Analysis — Algorithm Steps
Identify corner and edge positions
Calculate spreading potential for each position
Use greedy selection based on coverage
Verify solution completeness
Visualization
Tap to expand
Step-by-Step Walkthrough
1
Corner Analysis
Identify three corner positions with maximum spreading potential
2
Central Points
Add strategic middle positions to ensure connectivity
3
Verification
Confirm the selected positions enable full coverage
Code -
solution.c — C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
static int n;
static int triangles[100][2];
static int num_triangles;
static int current_combo[100][2];
static int result[100][2];
static int result_size;
static char red_set[1000][20];
static int red_count;
bool is_in_red_set(int row, int col) {
char key[20];
sprintf(key, "%d,%d", row, col);
for (int i = 0; i < red_count; i++) {
if (strcmp(red_set[i], key) == 0) return true;
}
return false;
}
void add_to_red_set(int row, int col) {
sprintf(red_set[red_count], "%d,%d", row, col);
red_count++;
}
int count_red_neighbors(int row, int col) {
int count = 0;
// Left neighbor
if (col > 1 && is_in_red_set(row, col - 1)) count++;
// Right neighbor
if (col < 2 * row - 1 && is_in_red_set(row, col + 1)) count++;
// Upper neighbor
if (row > 1 && is_in_red_set(row - 1, col / 2 + (col % 2))) count++;
// Lower neighbors
if (row < n) {
if (is_in_red_set(row + 1, 2 * col - 1)) count++;
if (is_in_red_set(row + 1, 2 * col)) count++;
}
return count;
}
bool can_color_all(int combo_size) {
red_count = 0;
// Add initial red triangles
for (int i = 0; i < combo_size; i++) {
add_to_red_set(current_combo[i][0], current_combo[i][1]);
}
bool changed = true;
while (changed) {
changed = false;
for (int i = 0; i < num_triangles; i++) {
int r = triangles[i][0];
int c = triangles[i][1];
if (!is_in_red_set(r, c)) {
if (count_red_neighbors(r, c) >= 2) {
add_to_red_set(r, c);
changed = true;
}
}
}
}
return red_count == num_triangles;
}
bool generate_combinations(int k, int start, int current_size) {
if (current_size == k) {
if (can_color_all(k)) {
result_size = k;
for (int i = 0; i < k; i++) {
result[i][0] = current_combo[i][0];
result[i][1] = current_combo[i][1];
}
return true;
}
return false;
}
for (int i = start; i <= num_triangles - (k - current_size); i++) {
current_combo[current_size][0] = triangles[i][0];
current_combo[current_size][1] = triangles[i][1];
if (generate_combinations(k, i + 1, current_size + 1)) {
return true;
}
}
return false;
}
int main() {
scanf("%d", &n);
// Generate all triangle coordinates
num_triangles = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < 2 * i; j++) {
triangles[num_triangles][0] = i;
triangles[num_triangles][1] = j;
num_triangles++;
}
}
// Try increasing k
for (int k = 1; k <= num_triangles; k++) {
if (generate_combinations(k, 0, 0)) {
break;
}
}
// Print result
printf("[");
for (int i = 0; i < result_size; i++) {
if (i > 0) printf(",");
printf("[%d,%d]", result[i][0], result[i][1]);
}
printf("]\n");
return 0;
}
Time & Space Complexity
Time Complexity
⏱️
O(n⁴)
For each of O(n²) positions, we simulate O(n²) spreading iterations
n
2n
✓ Linear Growth
Space Complexity
O(n²)
Store triangle coordinates and adjacency information
n
2n
⚠ Quadratic Space
12.5K Views
MediumFrequency
~45 minAvg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡Explanation
AI Ready
💡 SuggestionTabto acceptEscto dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen
Algorithm Visualization
Pinch to zoom • Tap outside to close
Test Cases
0 passed
0 failed
3 pending
Select Compiler
Choose a programming language
Compiler list would appear here...
AI Editor Features
Header Buttons
💡
Explain
Get a detailed explanation of your code. Select specific code or analyze the entire file. Understand algorithms, logic flow, and complexity.
🔧
Fix
Automatically detect and fix issues in your code. Finds bugs, syntax errors, and common mistakes. Shows you what was fixed.
💡
Suggest
Get improvement suggestions for your code. Best practices, performance tips, and code quality recommendations.
💬
Ask AI
Open an AI chat assistant to ask any coding questions. Have a conversation about your code, get help with debugging, or learn new concepts.
Smart Actions (Slash Commands)
🔧
/fix Enter
Find and fix issues in your code. Detects common problems and applies automatic fixes.
💡
/explain Enter
Get a detailed explanation of what your code does, including time/space complexity analysis.
🧪
/tests Enter
Automatically generate unit tests for your code. Creates comprehensive test cases.
📝
/docs Enter
Generate documentation for your code. Creates docstrings, JSDoc comments, and type hints.
⚡
/optimize Enter
Get performance optimization suggestions. Improve speed and reduce memory usage.
AI Code Completion (Copilot-style)
👻
Ghost Text Suggestions
As you type, AI suggests code completions shown in gray text. Works with keywords like def, for, if, etc.
Tabto acceptEscto dismiss
💬
Comment-to-Code
Write a comment describing what you want, and AI generates the code. Try: # two sum, # binary search, # fibonacci
💡
Pro Tip: Select specific code before using Explain, Fix, or Smart Actions to analyze only that portion. Otherwise, the entire file will be analyzed.