# Pattern

Solve Problem by Patterns at [Algomaster](https://algomaster.io/practice/dsa-patterns) | [thita](https://thita.ai/dsa-patterns-sheet) |&#x20;

![](/files/w0v0JhXtGam3efbNsdKV)

### **Prefix Sum** :&#x20;

The Prefix Sum pattern involves preprocessing an array to create a new array where each element at index i represents the sum of all elements from the start up to i. This allows for O(1) sum queries on any sub array. We can use this in following scenario :-&#x20;

1. Multiple sum queries on sub arrays
2. Finding sub arrays with a target sum
3. Calculating cumulative totals

References&#x20;

* <https://x.com/ashishps_1/status/2012864271773016266>
* <https://blog.algomaster.io/p/15-leetcode-patterns>&#x20;
* <https://leetcode.com/discuss/post/1326900/graph-algorithms-problems-to-practice-by-9u6j/>
* <https://leetcode.com/discuss/post/655708/graph-for-beginners-problems-pattern-sam-06fb/>&#x20;

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://59r.gitbook.io/ml-university/dsa-data-structure-and-algorithms/pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
