Find the maximum possible value of last element of the Array
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
#include <bits/stdc++.h>
using namespace std;
int maxValue( int arr[], int n, int moves)
{
for ( int i = n - 2; i >= 0; i--) {
if (arr[i] > 0) {
int distance = n - 1 - i;
if (moves < distance)
break ;
int can_take = moves / distance;
int take = min(arr[i], can_take);
arr[n - 1] += take;
moves -= take * distance;
}
}
return arr[n - 1];
}
int main()
{
int arr[] = { 2, 3, 0, 1 };
int M = 5;
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxValue(arr, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxValue( int arr[], int n, int moves)
{
for ( int i = n - 2 ; i >= 0 ; i--) {
if (arr[i] > 0 ) {
int distance = n - 1 - i;
if (moves < distance)
break ;
int can_take = moves / distance;
int take = Math.min(arr[i], can_take);
arr[n - 1 ] += take;
moves -= take * distance;
}
}
return arr[n - 1 ];
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 0 , 1 };
int M = 5 ;
int N = arr.length;
System.out.print(maxValue(arr, N, M));
}
}
|
Python3
def maxValue(arr, n, moves):
for i in range (n - 2 , - 1 , - 1 ):
if (arr[i] > 0 ):
distance = n - 1 - i
if (moves < distance):
break
can_take = moves / / distance
take = min (arr[i], can_take)
arr[n - 1 ] + = take
moves - = take * distance
return arr[n - 1 ]
if __name__ = = '__main__' :
arr = [ 2 , 3 , 0 , 1 ]
M = 5
N = len (arr)
print (maxValue(arr, N, M))
|
C#
using System;
class GFG{
static int maxValue( int []arr, int n, int moves)
{
for ( int i = n - 2; i >= 0; i--) {
if (arr[i] > 0) {
int distance = n - 1 - i;
if (moves < distance)
break ;
int can_take = moves / distance;
int take = Math.Min(arr[i], can_take);
arr[n - 1] += take;
moves -= take * distance;
}
}
return arr[n - 1];
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 0, 1 };
int M = 5;
int N = arr.Length;
Console.Write(maxValue(arr, N, M));
}
}
|
Javascript
<script>
function maxValue(arr, n, moves)
{
for ( var i = n - 2; i >= 0; i--)
{
if (arr[i] > 0)
{
var distance = n - 1 - i;
if (moves < distance)
break ;
var can_take = parseInt(moves / distance);
var take = Math.min(arr[i], can_take);
arr[n - 1] += take;
moves -= take * distance;
}
}
return arr[n - 1];
}
var arr = [2, 3, 0, 1];
var M = 5;
var N = arr.length;
document.write( maxValue(arr, N, M));
</script>
|
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1), constant extra space is required.
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...