# Algorithm

这个任务需要你注册并登陆：<https://practiceit.cs.washington.edu/>

如果你登陆成功，可以开始完成以下任务：

{% hint style="info" %}
注意：每道题目先点击右上角的“Show Header”，方便了解函数的参数和返回值。
{% endhint %}

1. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/s10-max>
2. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/s11-average>
3. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/s16a-countStrings>
4. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/e3-countInRange>
5. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/s16b-equalsStrings>
6. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/s17-allLess>
7. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/e1-lastIndexOf>
8. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/e10-percentEven>
9. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/e12-priceIsRight>
10. <https://practiceit.cs.washington.edu/problem/view/bjp5/chapter7/e16-append>

For Q10, you want to create a new array with appropriate length in the beginning of the function.

### Optional Question

#### OQ1: findLongestSubarray

Write a function **`int findLongestSubarray(int[] arr)`** that takes in an array of integers and returns the length of the longest subarray in which the numbers are in ascending order. For example, if the input array is {2, 6, 4, 8, 10, 15}, the function should return 4, because the longest subarray in which the numbers are in ascending order is {4, 8, 10, 15}.

Sample Code is below:

```java
public class Main {
  public static int findLongestSubarray(int[] arr) {
    // Your code here
  }

  public static void main(String[] args) {
    // Test the function with different input arrays
    int[] array = {2, 6, 4, 8, 10, 15};
    System.out.println(findLongestSubarray(array));// should return 4
  }
}
```

#### OQ2: findFirstDuplicate

Implement a function **int findFirstDuplicate(int\[] arr)** that takes in an array of integers and returns the first duplicate number in the array. If there are no duplicate numbers, the function should return -1.

For example, if the input array is {2, 3, 1, 5, 2, 4}, the function should return 2, because 2 is the first duplicate number in the array.

Sample Code is below:

```java
public class Main {
  public static int findFirstDuplicate(int[] arr) {
    // Your code here
    
  }

  public static void main(String[] args) {
    // Test the function with different input arrays
    int[] arr1 = {2, 3, 1, 5, 2, 4};
    int[] arr2 = {1, 2, 3, 4, 5};
    int[] arr3 = {5, 3, 2, 5, 1, 4};
    System.out.println(findFirstDuplicate(arr1)); // should return 2
    System.out.println(findFirstDuplicate(arr2)); // should return -1
    System.out.println(findFirstDuplicate(arr3)); // should return 5
  }
}
```


---

# 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://scls-cs.gitbook.io/scls-apcs-lab/hackathon-ii-array-algorithms/algorithm.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.
