Maximise minimum element possible in Array after performing given operations
Last Updated :
23 Jul, 2025
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]. and
- A value of 2 * x can be added to arr[i-2].
Find the maximum possible minimum element of the array after any such operations.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 3
Explanation: The last element is chosen and x =1 can be chosen.
So 3*x gets subtracted from arr[i] and x gets added to arr[i-1] and 2*x to arr[i-2] so the array becomes {1, 2, 3, 6, 6, 3}
In the 4th index x =1 can be chosen and now the array becomes {1, 2, 5, 7, 3, 3}.
In the 3rd index x = 1 can be chosen and now the array becomes {1, 4, 6, 4, 3, 3}.
In the 2nd index again x =1 can be chosen and now the array becomes {3, 4, 3, 4, 3, 3, 3}.
Hence the maximum possible minimum value is 3.
Input: arr[] = {9, 13, 167}
Output: 51
Naive Approach: This problem can be solved by checking for the possibility of maximum possible minimum value from [1, max(array)] by performing the operations from the end of the array.
Time Complexity: O(N2)
Space Complexity: O(1)
Efficient Approach: The efficient approach of this problem is based on Binary Search. Since it is based on maximizing the minimum value so by applying the binary search in the range [1, max(array)] and checking if mid is possible as a minimum element by performing the operations on the array such that every element is >=mid. Follow the steps below to solve the given problem:
- Initialize f = 1 and l = maximum element of the array and the res as INT_MIN.
- Perform binary search while f<=l
- Check if mid can be the minimum element by performing operations in the is_possible_min() function.
- In the is_possible_min() function
- Traverse from the end of the array (N-1) till index 2 and check if arr[i]<mid if it true return 0.
- Else find the extra which is 3x that can be added to arr[i-1] as x and arr[i-2] as 2x.
- If arr[0] >=mid and arr[1] >=mid return 1.
- Else return 0.
- If the is_possible_min() function returns true then mid is possible as the minimum value store the max(res, mid) in the res variable, so maximize the minimum value by moving right as f=mid +1
- Else move towards left and try if it is possible by l = mid -1.
- Print the res.
Below is the implementation of the above approach.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Check if mid is possible as
// a minimum element after
// any number of operations using
// this predicate function
bool is_possible_min(vector<int> arr,
int mid)
{
int N = arr.size();
// Traverse from the end
for (int i = N - 1; i >= 2; i--) {
// mid can't be minimum
if (arr[i] < mid)
return 0;
else {
// Find the 3x
int extra = arr[i] - mid;
// Find the x
extra /= 3;
// Add x to a[i-1]
arr[i - 1] += extra;
// Add 2x to a[i-2]
arr[i - 2] += 2 * extra;
}
}
// Check if the first two elements
// are >= mid because if every element
// is greater than or equal to
// mid we can conclude
// mid as a minimum element
if (arr[0] >= mid && arr[1] >= mid)
return 1;
return 0;
}
// Function to find the
// maximum possible minimum value
void find_maximum_min(vector<int> arr)
{
// Initialize f = 1 and l as the
// maximum element of the array
int f = 1, l = *max_element(arr.begin(),
arr.end());
// Initialize res as INT_MIN
int res = INT_MIN;
// Perform binary search while f<=l
while (f <= l) {
int mid = (f + l) / 2;
// Check if mid is possible
// as a minimum element
if (is_possible_min(arr, mid)) {
// Take the max value of mid
res = max(res, mid);
// Try maximizing the min value
f = mid + 1;
}
// Move left if it is not possible
else {
l = mid - 1;
}
}
// Print the result
cout << res << endl;
}
// Driver Code
int main()
{
// Initialize the array
vector<int> arr = { 1, 2, 3, 4, 5, 6 };
// Function call
find_maximum_min(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Check if mid is possible as
// a minimum element after
// any number of operations using
// this predicate function
static Boolean is_possible_min(int arr[],
int mid)
{
int N = arr.length;
// Traverse from the end
for (int i = N - 1; i >= 2; i--) {
// mid can't be minimum
if (arr[i] < mid)
return false;
else {
// Find the 3x
int extra = arr[i] - mid;
// Find the x
extra /= 3;
// Add x to a[i-1]
arr[i - 1] += extra;
// Add 2x to a[i-2]
arr[i - 2] += 2 * extra;
}
}
// Check if the first two elements
// are >= mid because if every element
// is greater than or equal to
// mid we can conclude
// mid as a minimum element
if (arr[0] >= mid && arr[1] >= mid)
return true;
return false;
}
// Function to find the
// maximum possible minimum value
static void find_maximum_min(int arr[])
{
// Initialize f = 1 and l as the
// maximum element of the array
int f = 1, l = Arrays.stream(arr).max().getAsInt();
// Initialize res as INT_MIN
int res = Integer.MIN_VALUE;
// Perform binary search while f<=l
while (f <= l) {
int mid = l + (f - l) / 2;
// Check if mid is possible
// as a minimum element
if (is_possible_min(arr, mid) == true) {
// Take the max value of mid
res = Math.max(res, mid);
// Try maximizing the min value
f = mid + 1;
}
// Move left if it is not possible
else {
l = mid - 1;
}
}
// Print the result
System.out.println(res);
}
// Driver Code
public static void main (String[] args)
{
// Initialize the array
int arr[] = { 1, 2, 3, 4, 5, 6 };
// Function call
find_maximum_min(arr);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 program for the above approach
INT_MIN = -2147483647 - 1
# Check if mid is possible as
# a minimum element after
# any number of operations using
# this predicate function
def is_possible_min(arr, mid):
N = len(arr)
# Traverse from the end
for i in range(N-1, 1, -1):
# mid can't be minimum
if (arr[i] < mid):
return 0
else:
# Find the 3x
extra = arr[i] - mid
# Find the x
extra //= 3
# Add x to a[i-1]
arr[i - 1] += extra
# Add 2x to a[i-2]
arr[i - 2] += 2 * extra
# Check if the first two elements
# are >= mid because if every element
# is greater than or equal to
# mid we can conclude
# mid as a minimum element
if (arr[0] >= mid and arr[1] >= mid):
return 1
return 0
# Function to find the
# maximum possible minimum value
def find_maximum_min(arr):
# Initialize f = 1 and l as the
# maximum element of the array
f, l = 1, max(arr)
# Initialize res as INT_MIN
res = INT_MIN
# Perform binary search while f<=l
while (f <= l):
mid = (f + l) // 2
# print(is_possible_min(arr,mid))
# Check if mid is possible
# as a minimum element
if (is_possible_min(arr.copy(), mid)):
# Take the max value of mid
res = max(res, mid)
# Try maximizing the min value
f = mid + 1
# Move left if it is not possible
else:
l = mid - 1
# Print the result
print(res)
# Driver Code
if __name__ == "__main__":
# Initialize the array
arr = [1, 2, 3, 4, 5, 6]
# Function call
find_maximum_min(arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG
{
// Check if mid is possible as
// a minimum element after
// any number of operations using
// this predicate function
static bool is_possible_min(int[] arr, int mid)
{
int N = arr.Length;
// Traverse from the end
for (int i = N - 1; i >= 2; i--) {
// mid can't be minimum
if (arr[i] < mid)
return false;
else {
// Find the 3x
int extra = arr[i] - mid;
// Find the x
extra /= 3;
// Add x to a[i-1]
arr[i - 1] += extra;
// Add 2x to a[i-2]
arr[i - 2] += 2 * extra;
}
}
// Check if the first two elements
// are >= mid because if every element
// is greater than or equal to
// mid we can conclude
// mid as a minimum element
if (arr[0] >= mid && arr[1] >= mid)
return true;
return false;
}
// Function to find the
// maximum possible minimum value
static void find_maximum_min(int[] arr)
{
// Initialize f = 1 and l as the
// maximum element of the array
int f = 1, l = arr.Max();
// Initialize res as INT_MIN
int res = Int32.MinValue;
// Perform binary search while f<=l
while (f <= l) {
int mid = l + (f - l) / 2;
// Check if mid is possible
// as a minimum element
if (is_possible_min(arr, mid) == true) {
// Take the max value of mid
res = Math.Max(res, mid);
// Try maximizing the min value
f = mid + 1;
}
// Move left if it is not possible
else {
l = mid - 1;
}
}
// Print the result
Console.WriteLine(res);
}
// Driver Code
public static void Main()
{
// Initialize the array
int[] arr = { 1, 2, 3, 4, 5, 6 };
// Function call
find_maximum_min(arr);
}
}
// This code is contributed by Taranpreet
JavaScript
<script>
// Javascript program for the above approach
// Check if mid is possible as
// a minimum element after
// any number of operations using
// this predicate function
function is_possible_min(arr, mid) {
let N = arr.length;
// Traverse from the end
for (let i = N - 1; i >= 2; i--) {
// mid can't be minimum
if (arr[i] < mid)
return 0;
else {
// Find the 3x
let extra = arr[i] - mid;
// Find the x
extra = Math.floor(extra / 3);
// Add x to a[i-1]
arr[i - 1] += extra;
// Add 2x to a[i-2]
arr[i - 2] += 2 * extra;
}
}
// Check if the first two elements
// are >= mid because if every element
// is greater than or equal to
// mid we can conclude
// mid as a minimum element
if (arr[0] >= mid && arr[1] >= mid)
return 1;
return 0;
}
// Function to find the
// maximum possible minimum value
function find_maximum_min(arr) {
// Initialize f = 1 and l as the
// maximum element of the array
let f = 1, l = max_element(arr);
// Initialize res as INT_MIN
let res = Number.MIN_SAFE_INTEGER
// Perform binary search while f<=l
while (f <= l) {
let mid = Math.ceil((f + l) / 2);
// Check if mid is possible
// as a minimum element
if (is_possible_min(arr, mid)) {
// Take the max value of mid
res = Math.max(res, mid);
// Try maximizing the min value
f = mid + 1;
}
// Move left if it is not possible
else {
l = mid - 1;
}
}
// Print the result
document.write(res);
}
function max_element(ar) {
return [...ar].sort((a, b) => - a + b)[0]
}
// Driver Code
// Initialize the array
let arr = [1, 2, 3, 4, 5, 6];
// Function call
find_maximum_min(arr);
// This code is contributed by saurabh_jaiswal.
</script>
Time Complexity: O(N* log(maxval)) where maxval is the maximum element of the array. As we are using a while loop to traverse log(maxval) times as we are decrementing by floor division of 2 every traversal, and is_possible_min function will cost O (N) as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem