> For the complete documentation index, see [llms.txt](https://nissan-goldberg.gitbook.io/java101/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nissan-goldberg.gitbook.io/java101/lesson-7/more-exercises.md).

# More Exercises

### Binary search- recursively

```java
public class Main {
    //wrapper function
    public static int binarySearch(int[] arr, int x) {
        int result = bSearch(arr, 0, arr.length - 1, x);
        if (result == -1)
            System.out.println("Element not present");
       return result;
    }

    // Recursive function: Returns index of element or -1 if not found
    public static int bSearch(int[] arr, int l, int r, int x) {
        if (r >= l && l < arr.length-1) {
            int mid = l + (r - l) / 2;

            if (arr[mid] == x)  // If the element is in the middle
                return mid;

            if (arr[mid] > x) //elem is smaller than middle elem
                return bSearch(arr, l, mid - 1, x);
            else             //elem is larger than middle elem
                return bSearch(arr, mid + 1, r, x);
        }

        return -1;  //not present in array
    }

    public static void main(String args[]) {
        int arr[] = { 2, 4, 8, 16, 50 };
        int x = 16;
        int res = binarySearch(arr, x);
        System.out.println(res);
    }
}
```

## Exercise - Replace

```
replace("world", "or", "") -> wrl
replace("python program python program", "python", "java") -> java program java program
```

## Solution

```java
public class Main {
    public static String replace(String str, String from, String to){
        if (str.length() < from.length() ) //don't need to check anymore
            return str;

        if (str.startsWith(from))
            return to + replace(str.substring(from.length()), from, to);
        else //if doesnt start with
            return str.charAt(0) + replace(str.substring(1), from, to);
    }

    public static void main(String[] args) {
        System.out.println(replace("world", "or", ""));
        System.out.println(replace("world", "or", "ZZZ"));
        System.out.println(replace("i am a python program i am a python program", "python", "java"));
    }
}
```

```
wld
wZZZld
i am a java program i am a java program
```

notice here

```java
if (str.startsWith(from))
            return to + replace(str.substring(from.length()), from, to);
```

we start `substring` from `from.length()`

and here

```java
else
     return str.charAt(0) + replace(str.substring(1), from, to);
```

we start `substring` from **1**


---

# 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://nissan-goldberg.gitbook.io/java101/lesson-7/more-exercises.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.
