Given an array **arr[]** of **N** integers, the task is to remove the elements from both the ends of the array i.e. in a single operation, either the first or the last element can be removed from the current remaining elements of the array. This operation needs to be performed in such a way that the last element left will have the minimum possible value. Print this minimum value.

**Examples:**

Input:arr[] = {5, 3, 1, 6, 9}

Output:1

Operation 1: arr[] = {5, 3, 1, 6}

Operation 2: arr[] = {5, 3, 1}

Operation 3: arr[] = {3, 1}

Operation 4: arr[] = {1}

Input:arr[] = {2, 6, 4, 8, 2, 6}

Output:2

**Approach:** This problem can be solved greedily, the element with the maximum value from either of the end needs to be removed in a single operation. Following this approach until only one element is left in the array will give us the minimum element from the original array in the end.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum possible ` `// value of the last element left after ` `// performing the given operations ` `int` `getMin(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `minVal = *min_element(arr, arr + n); ` ` ` `return` `minVal; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 3, 1, 6, 9 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << getMin(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum possible ` `// value of the last element left after ` `// performing the given operations ` `static` `int` `getMin(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `minVal = Arrays.stream(arr).min().getAsInt(); ` ` ` `return` `minVal; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `5` `, ` `3` `, ` `1` `, ` `6` `, ` `9` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(getMin(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum possible ` `# value of the last element left after ` `# performing the given operations ` `def` `getMin(arr, n) : ` ` ` ` ` `minVal ` `=` `min` `(arr); ` ` ` `return` `minVal; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `5` `, ` `3` `, ` `1` `, ` `6` `, ` `9` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `print` `(getMin(arr, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` `using` `System.Linq; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum possible ` `// value of the last element left after ` `// performing the given operations ` `static` `int` `getMin(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `minVal = arr.Min(); ` ` ` `return` `minVal; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 5, 3, 1, 6, 9 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(getMin(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum Possible Product in Array after performing given Operations
- Maximum count of equal numbers in an array after performing given operations
- Maximum sum of all elements of array after performing given operations
- Find the modified array after performing k operations of given type
- Number of elements from the array which are reachable after performing given operations on D
- Find the final sequence of the array after performing given operations
- Maximum number of unique values in the array after performing given operations
- Maximum score possible after performing given operations on an Array
- Maximum possible Array sum after performing given operations
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Maximum inversions in a sequence of 1 to N after performing given operations at most K times
- Number of array elements derivable from D after performing certain operations
- Minimum possible sum of array elements after performing the given operation
- Minimum possible sum of array elements after performing the given operation
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Check if the last element of array is even or odd after performing a operation p times
- Find Array formed by adding each element of given array with largest element in new array to its left
- Maximum value obtained by performing given operations in an Array
- Arrange the array such that upon performing given operations an increasing order is obtained
- Maximize sum of array elements removed by performing the given operations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.