Find the maximum possible value of last element of the Array
Last Updated :
12 Jul, 2025
Given a non-negative array arr of size N and an integer M representing the number of moves such that in one move, the value of any one element in the array decreases by one, and the value of its adjacent element on the right increases by one. The task is to find the maximum possible value of the last element of the array in given M number of moves.
Examples:
Input: arr[] = {2, 3, 0, 1}, M = 5
Output: 3
Move 1: Working on index 1, the element 3 at 1st index reduces to 2 and the element 0 at 2nd index increases to 1. Hence the resultant array after one move = {2, 2, 1, 1}
Move 2: Working on index 2, the element 1 at 2nd index reduces to 0 and the element 1 at 3rd index increases to 2. Hence the resultant array after two moves = {2, 2, 0, 2}
Move 3: Working on index 1, the element 2 at 1st index reduces to 1 and the element 0 at 2nd index increases to 1. Hence the resultant array after three moves {2, 1, 1, 2}
Move 4: Working on index 2, the element 1 at 2nd index reduces to 0 and the element 2 at 3rd index increases to 3. Hence the resultant array after four moves {2, 1, 0, 3}
Move 5: Working on index 1, the element 1 at 1st index reduces to 0 and the element 0 at 2nd index increases to 1. Hence the resultant after five moves {2, 0, 1, 3}
So the maximum value of last element after 5 moves is 3
Input: arr[] = {1, 100}, M = 2
Output: 101
Approach:
The number of moves required to move one value from one element to the last element is calculated by the distance between them. For each element in the array, if the distance between this element and the final element is less than equal to M, then this element can be moved to the last. So in order to move it, increase the last element with the distance and reduce the left number of moves with the distance.
Below is the implementation of the above approach:
CPP
// C++ program to find the maximum possible
// value of last element of the array
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum possible
// value of last element of the array
int maxValue(int arr[], int n, int moves)
{
// Traverse for all element
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > 0) {
// Find the distance
int distance = n - 1 - i;
// If moves less than distance then
// we can not move this number to end
if (moves < distance)
break;
// How many number we can move to end
int can_take = moves / distance;
// Take the minimum of both of them
int take = min(arr[i], can_take);
// Increment in the end
arr[n - 1] += take;
// Remove taken moves
moves -= take * distance;
}
}
// Return the last element
return arr[n - 1];
}
// Driver code
int main()
{
int arr[] = { 2, 3, 0, 1 };
int M = 5;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxValue(arr, N, M);
return 0;
}
Java
// Java program to find the maximum possible
// value of last element of the array
import java.util.*;
class GFG{
// Function to find the maximum possible
// value of last element of the array
static int maxValue(int arr[], int n, int moves)
{
// Traverse for all element
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > 0) {
// Find the distance
int distance = n - 1 - i;
// If moves less than distance then
// we can not move this number to end
if (moves < distance)
break;
// How many number we can move to end
int can_take = moves / distance;
// Take the minimum of both of them
int take = Math.min(arr[i], can_take);
// Increment in the end
arr[n - 1] += take;
// Remove taken moves
moves -= take * distance;
}
}
// Return the last element
return arr[n - 1];
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 3, 0, 1 };
int M = 5;
int N = arr.length;
// Function call
System.out.print(maxValue(arr, N, M));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find the maximum possible
# value of last element of the array
# Function to find the maximum possible
# value of last element of the array
def maxValue(arr, n, moves):
# Traverse for all element
for i in range(n - 2, -1, -1):
if (arr[i] > 0):
# Find the distance
distance = n - 1 - i
# If moves less than distance then
# we can not move this number to end
if (moves < distance):
break
# How many number we can move to end
can_take = moves // distance
# Take the minimum of both of them
take = min(arr[i], can_take)
# Increment in the end
arr[n - 1] += take
# Remove taken moves
moves -= take * distance
# Return the last element
return arr[n - 1]
# Driver code
if __name__ == '__main__':
arr= [2, 3, 0, 1]
M = 5
N = len(arr)
# Function call
print(maxValue(arr, N, M))
# This code is contributed by mohit kumar 29
C#
// C# program to find the maximum possible
// value of last element of the array
using System;
class GFG{
// Function to find the maximum possible
// value of last element of the array
static int maxValue(int []arr, int n, int moves)
{
// Traverse for all element
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > 0) {
// Find the distance
int distance = n - 1 - i;
// If moves less than distance then
// we can not move this number to end
if (moves < distance)
break;
// How many number we can move to end
int can_take = moves / distance;
// Take the minimum of both of them
int take = Math.Min(arr[i], can_take);
// Increment in the end
arr[n - 1] += take;
// Remove taken moves
moves -= take * distance;
}
}
// Return the last element
return arr[n - 1];
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 2, 3, 0, 1 };
int M = 5;
int N = arr.Length;
// Function call
Console.Write(maxValue(arr, N, M));
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Javascript program to find the maximum possible
// value of last element of the array
// Function to find the maximum possible
// value of last element of the array
function maxValue(arr, n, moves)
{
// Traverse for all element
for (var i = n - 2; i >= 0; i--)
{
if (arr[i] > 0)
{
// Find the distance
var distance = n - 1 - i;
// If moves less than distance then
// we can not move this number to end
if (moves < distance)
break;
// How many number we can move to end
var can_take = parseInt(moves / distance);
// Take the minimum of both of them
var take = Math.min(arr[i], can_take);
// Increment in the end
arr[n - 1] += take;
// Remove taken moves
moves -= take * distance;
}
}
// Return the last element
return arr[n - 1];
}
// Driver code
var arr = [2, 3, 0, 1];
var M = 5;
var N = arr.length;
// Function call
document.write( maxValue(arr, N, M));
// This code is contributed by rutvik_56.
</script>
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1), constant extra space is required.
Similar Reads
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
Find the element having maximum premutiples in the array Given an array arr[], the task is to find the element which has the maximum number of pre-multiples present in the set. For any index i, pre-multiple is the number that is multiple of i and is present before the ith index of the array. Also, print the count of maximum multiples of that element in th
8 min read
Find the position of the last removed element from the array Given an array of size N and an integer M . Perform the following operations on the given array: If a[i] > M then push a[i] - M to end of the array, otherwise remove it from the array.Perform the first operation while the array is non-empty. The task is to find the original position of the elemen
6 min read
Maximum in array which is at-least twice of other elements Given an array of integers of length n. Our task is to return the index of the max element if the it is at least twice as much as every other number in the array. If the max element does not satisfy the condition return -1. Examples: Input : arr = {3, 6, 1, 0} Output : 1 Here, 6 is the largest integ
7 min read
Find the maximum possible value of a[i] % a[j] over all pairs of i and j Given an array arr[] of N positive integers. The task is to find the maximum possible value of a[i] % a[j] over all pairs of i and j. Examples: Input: arr[] = {4, 5, 1, 8} Output: 5 If we choose the pair (5, 8), then 5 % 8 gives us 5 which is the maximum possible. Input: arr[] = {7, 7, 8, 8, 1} Outp
6 min read
Find the maximum elements in the first and the second halves of the Array , Given an array arr[] of N integers. The task is to find the largest elements in the first half and the second half of the array. Note that if the size of the array is odd then the middle element will be included in both halves.Examples: Input: arr[] = {1, 12, 14, 5} Output: 12, 14 First half is {1
6 min read