Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Maximise minimum element possible in Array after performing given operations
Next article icon

Minimum element left from the array after performing given operations

Last Updated : 29 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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>
 
 
Output: 
1

 

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.



Next Article
Maximise minimum element possible in Array after performing given operations

K

KunalGupta
Improve
Article Tags :
  • Arrays
  • DSA
  • Greedy
Practice Tags :
  • Arrays
  • Greedy

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
  • Find the modified array after performing k operations of given type
    Given an array arr[] of n integers and an integer K. In one operation every element of the array is replaced by the difference of that element and the maximum value of the array. The task is to print the array after performing K operations.Examples: Input: arr[] = {4, 8, 12, 16}, k = 2 Output: 0 4 8
    10 min read
  • Find maximum value of the last element after reducing the array with given operations
    Given an array arr[] of N elements, you have to perform the following operation on the given array until the array is reduced to a single elements, Choose two indices i and j such that i != j.Replace arr[i] with arr[i] - arr[j] and remove arr[j] from the array. The task is to maximize and print the
    7 min read
  • Make the array elements equal by performing given operations minimum number of times
    Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: a
    7 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
  • Maximum score possible after performing given operations on an Array
    Given an array A of size N, the task is to find the maximum score possible of this array. The score of an array is calculated by performing the following operations on the array N times: If the operation is odd-numbered, the score is incremented by the sum of all elements of the current array. If th
    15+ min read
  • Maximize sum of array elements removed by performing the given operations
    Given two arrays arr[] and min[] consisting of N integers and an integer K. For each index i, arr[i] can be reduced to at most min[i]. Consider a variable, say S(initially 0). The task is to find the maximum value of S that can be obtained by performing the following operations: Choose an index i an
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences