# Lower Bound in Java

In Java, the concept of lower bound is commonly associated with the lower_bound() method, which is frequently used in algorithms to find the index of the first element in an array that is greater than or equal to a specified key. This is particularly useful when working with sorted data and searching for the position where a new element should be inserted while maintaining the sorted order.

Let's explore different methods to implement the lower bound in Java and understand their time complexities and use cases.

### Method 1: Naive Approach - Linear Search

The naive approach involves a simple linear search through the array. The method compares each element with the key until it finds a value greater than or equal to the key. The index of this element is then returned as the lower bound.

File Name: LowerBoundExample1.java

Output:

```Lower bound for element 18 at index 4
```

Time Complexity: O(N), where N is the number of elements in the array.

Auxiliary Space: O(1)

This method is simple but not optimal for large datasets. Let's explore more efficient methods.

### Method 2: Binary Search Iteratively

Using binary search provides a more efficient solution for finding the lower bound. This approach works on the principle of repeatedly dividing the search interval in half.

File Name: LowerBoundExample2.java

Output:

```Lower bound for element 18 at index 4
```

Time Complexity: O(logN)

Auxiliary Space: O(1)

This method significantly reduces the time complexity compared to linear search. However, there's a more concise and readable way to achieve the same result using recursion.

### Method 3: Binary Search Recursively

This method utilizes recursion to implement the binary search, making the code more modular.

File Name: LowerBoundExample3.java

Output:

```Lower bound for element 18 at index 4
```

Time Complexity: O(logN)

Auxiliary Space: O(logN)

This method provides the same time complexity as the iterative binary search but with a slightly higher space complexity due to the recursive calls.

### Method 4: Using Arrays.binarySearch()

Java provides a built-in method in the Arrays utility class called binarySearch(), which can be used to find the lower bound efficiently.

File Name: LowerBoundExample4.java

Output:

```Lower bound for element 18 at index 4
```

Time Complexity: O(logN)

Auxiliary Space: O(1)

Using Arrays.binarySearch() simplifies the code and provides an efficient solution. It returns the lower bound directly or the insertion point in case the key is not present.

In conclusion, the choice of method depends on the specific requirements of our application. The iterative binary search and Arrays.binarySearch() methods are generally preferred for their efficiency, while the recursive binary search offers a more modular and readable implementation. Choose the method that best fits the needs of our project.