Minimum element left from the array after performing given operations
Last Updated :
29 May, 2022
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;
}
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
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
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
JavaScript
<script>
// javascript implementation of the above approach
// Function to return the minimum possible
// value of the last element left after
// performing the given operations
function getMin(arr, n)
{
var minVal = Math.min.apply(Math, arr);
return minVal;
}
// Driver code
var arr = [ 5, 3, 1, 6, 9 ];
var n = arr.length;
document.write(getMin(arr, n));
// This code is contributed by aashish1995
</script>
Time Complexity: O(N), as we are using inbuilt min_element function which will cost us O(N) time.
Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Maximise minimum element possible in Array after performing given operations Given an array arr[] of size N. The task is to maximize the minimum value of the array after performing given operations. In an operation, value x can be chosen and A value 3 * x can be subtracted from the arr[i] element.A value x is added to arr[i-1]. andA value of 2 * x can be added to arr[i-2]. F
11 min read
Minimum possible sum of array elements after performing the given operation Given an array arr[] of size N and a number X. If any sub array of the array(possibly empty) arr[i], arr[i+1], ... can be replaced with arr[i]/x, arr[i+1]/x, .... The task is to find the minimum possible sum of the array which can be obtained. Note: The given operation can only be performed once.Exa
9 min read
Minimum operations to make Array sum at most S from given Array Given an array arr[], of size N and an integer S, the task is to find the minimum operations to make the sum of the array less than or equal to S. In each operation: Any element can be chosen and can be decremented by 1, orCan be replaced by any other element in the array. Examples: Input: arr[]= {1
10 min read
Minimum Cost to make all array elements equal using given operations Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t
12 min read
Minimize operations of removing 2i -1 array elements to empty given array Given an array arr[] of size N, the task is to empty given array by removing 2i - 1 array elements in each operation (i is any positive integer). Find the minimum number of operations required. Examples: Input: arr[] = { 2, 3, 4 } Output: 1 Explanation: Removing (22 - 1) elements i.e { arr[0], arr[1
5 min read