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
// 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 arrayint 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 codeint 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 arrayimport java.util.*;Â
class GFG{  // Function to find the maximum possible// value of last element of the arraystatic 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 codepublic 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 arraydef 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 codeif __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 arrayusing System;Â
class GFG{   // Function to find the maximum possible// value of last element of the arraystatic 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 codepublic 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 arrayfunction 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 codevar arr = [2, 3, 0, 1];var M = 5;var N = arr.length;Â
// Function calldocument.write( maxValue(arr, N, M));Â
// This code is contributed by rutvik_56.</script> |
3
Â
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1), constant extra space is required.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



