⌨️ Codingadvancedalgorithmsperformancebig-ooptimization

Optimize an Algorithm for Speed or Memory

Analyze your algorithm's time and space complexity and get an optimized version with explanation.

The Prompt

prompt.txt
Analyze and optimize the following algorithm. Provide:
1. Current time complexity (Big O notation) and explanation
2. Current space complexity
3. The bottleneck — where is most time/memory being spent?
4. An optimized version with improved complexity
5. New time and space complexity after optimization
6. Trade-offs: what did we sacrifice to gain speed/memory?

Optimization target: [SPEED / MEMORY / BOTH]

Algorithm:
```
[PASTE YOUR CODE HERE]
```

Typical input size: [e.g., n ≈ 10,000 — 1,000,000]

Example Output

Current complexity O(n²) due to the nested loop checking all pairs. Optimized to O(n log n) using a sorted array + two-pointer technique, eliminating the inner loop entirely. Memory stays O(1) extra. The trade-off is that the array must be sorted first, which changes the element ordering.

FAQ

Which AI model is best for Optimize an Algorithm for Speed or Memory?

Claude Sonnet 4 or GPT-4o. For competitive programming problems, o4-mini with extended thinking is excellent.

How do I use the Optimize an Algorithm for Speed or Memory prompt?

Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Current complexity O(n²) due to the nested loop checking all pairs. Optimized to O(n log n) using a sorted array + two-pointer technique, eliminating the inner loop entirely. Memory stays O(1) extra. The trade-off is that the array must be sorted first, which changes the element ordering.