> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook/13.-asymptotics-i/13.3-checkpoint-an-exercise.md).

# 13.3 Checkpoint: An Exercise

Exercise: Apply techniques 2A and 2B to `dup2`.

* Calculate the counts of each operation for the following code with respect to N.
* Predict the ***rough*** magnitudes of each one.

```java
for (int i = 0; i < A.length - 1; i += 1){
  if (A[i] == A[i + 1]) { 
    return true; 
  }
}
return false;
```

{% embed url="<https://www.youtube.com/watch?ab_channel=JoshHug&v=IxQh6SXRBRw>" %}

#### Solution:

Note: It's okay if you were slightly off—as mentioned earlier, you want ***rough*** estimates.

| Operation      | Symbolic Count | Count (for N=10000) |
| -------------- | -------------- | ------------------- |
| i = 0          | 1              | 1                   |
| j = i+1        | 0 to $$N$$     | 0 to 10,000         |
| <              | 0 to $$N-1$$   | 0 to 9,999          |
| ==             | 1 to $$N-1$$   | 1 to 9,999          |
| array accesses | 2 to $$2N-2$$  | 2 to 19998          |

{% embed url="<https://www.youtube.com/watch?ab_channel=JoshHug&v=jms0P6p6aQc>" %}
"I have another problem for you to solve ( ͡° ͜ʖ ͡°)..." - Josh Hug
{% endembed %}

Let us compare the `dup1` table with the `dup2` table:

`dup1` table:

| Operation      | Symbolic Count           | Count (for N=10000)                               |
| -------------- | ------------------------ | ------------------------------------------------- |
| i = 0          | 1                        | 1                                                 |
| j = i+1        | 1 to $$N$$               | 1 (in the best case) to 10000 (in the worst case) |
| <              | 2 to $$(N² + 3N + 2)/2$$ | 2 to 50,015,001                                   |
| += 1           | 0 to $$(N² + N)/2$$      | 0 to 50,005,000                                   |
| ==             | 1 to $$(N² - N)/2$$      | 1 to 49,995,000                                   |
| array accesses | 2 to $$N²-N$$            | 2 to 99,990,000                                   |

`dup2` table:

| Operation      | Symbolic Count | Count (for N=10000) |
| -------------- | -------------- | ------------------- |
| i = 0          | 1              | 1                   |
| j = i+1        | 0 to $$N$$     | 0 to 10,000         |
| <              | 0 to $$N-1$$   | 0 to 9,999          |
| ==             | 1 to $$N-1$$   | 1 to 9,999          |
| array accesses | 2 to $$2N-2$$  | 2 to 19998          |

We can see that `dup2` performs significantly better than `dup1` in the worst case!&#x20;

One way to rationalize this is that it takes fewer operations for `dup2` to accomplish the same goal as `dup1`.&#x20;

A better realization is that the algorithm for `dup2` scales much better in the worst case (e.g. ($$N² + 3N + 2)/2$$ vs $$N$$)

An even **better** realization is that parabolas ($$N²$$) always grow faster than lines ($$N$$).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook/13.-asymptotics-i/13.3-checkpoint-an-exercise.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
