# Remove minimum elements from either side such that 2\*min becomes more than max

Given an unsorted array, trim the array such that twice of minimum is greater than maximum in the trimmed array. Elements should be removed either end of the array.

Number of removals should be minimum.

**Examples:**

```
arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200}
Output: 4
We need to remove 4 elements (4, 5, 100, 200)
so that 2*min becomes more than max.


arr[] = {4, 7, 5, 6}
Output: 0
We don't need to remove any element as 
4*2 > 7 (Note that min = 4, max = 8)

arr[] = {20, 7, 5, 6}
Output: 1
We need to remove 20 so that 2*min becomes
more than max

arr[] = {20, 4, 1, 3}
Output: 3
We need to remove any three elements from ends
like 20, 4, 1 or 4, 1, 3 or 20, 3, 1 or 20, 4, 1
```

```java
public class solution {
    public minRemovalsDP(int arr[], int n) {
        int longest_start = -1, longest_end = 0;
        // finding longest continous sub-array which has 2*min>max
        for (int start = 0; start < n; start++) {
            int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
            // the memorisation lies in using previous end's result to get min and max of current end's result
            for (int end = start; end < n; end++) {
                min = Math.min(min, arr[end]);
                max = Math.max(max, arr[end]);
                // If the property is violated, then no point to continue for a bigger array
                if (2 * min <= max)
                    break;

                if (end - start > longest_end - longest_start || longest_start == -1) {
                    longest_start = start;
                    longest_end = end;
                }
            }
        }
        if (longest_start == -1)
            return n;

        return (n - (longest_end - longest_start + 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:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/hashmap-and-hashset-and-sliding-window/untitled-11.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.
