Given an array arr[ ] of size N and an integer p, the task is to find maximum possible value of rightmost element of array arr[ ] performing at most k operations.In one operation decrease arr[i] by p and increase arr[i+1] by p.
Examples:
Input: N = 4, p = 2, k = 5, arr = {3, 8, 1, 4}
Output: 8
Explanation:
following operation can be performed to achieve max valued rightmost element:
1. decrease arr[2] by 2 and increase arr[3] by 2, arr = {3, 6, 3, 4}
2. decrease arr[2] by 2 and increase arr[3] by 2, arr = {3, 4, 5, 4}
3. decrease arr[2] by 2 and increase arr[3] by 2, arr = {3, 2, 5, 4}
4. decrease arr[3] by 2 and increase arr[4] by 2, arr = {3, 2, 3, 6}
5. decrease arr[3] by 2 and increase arr[4] by 2, arr = {3, 2, 1, 8}Hence, maximum possible value of rightmost element will be 8.
Input: N = 5, p = 1, k = 2, arr = {1, 2, 3, 4, 5}
Output: 7
Naive Approach: This problem can solved naively using greedy approach. follow the steps below to solve the problem:
- Run a while loop, until is k is greater than 0.
- Run a for loop inside while loop, for i in range [N-2, 0].
- Check if arr[i] is greater than 1, increase arr[i+1] by p and decrease arr[i] by p and break for loop.
- Decrease the value of k by 1.
- Print arr[N-1].
Below is the implementation of the above approach:
C++
// C++ program for above approach #include <iostream> using namespace std; // Function to calculate maximum value of // Rightmost element void maxRightmostElement( int N, int k, int p, int arr[]){ // Calculating maximum value of // Rightmost element while (k) { for ( int i=N-2;i>=0;i--) { // Checking if arr[i] is operationable if (arr[i]>= p) { // Performing operation of i-th element arr[i]=arr[i]-p; arr[i+1]=arr[i+1]+p; break ; } } // Decreasing the value of k by 1 k--; } // Printing rightmost element cout<<arr[N-1]<<endl; } // Driver Code int main() { // Given Input int N = 4, p = 2, k = 5, arr[] = {3, 8, 1, 4}; // Function Call maxRightmostElement(N, k, p, arr); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to calculate maximum value of // Rightmost element static int maxRightmostElement( int N, int k, int p, int arr[]) { // Calculating maximum value of // Rightmost element while (k > 0 ) { for ( int i = N - 2 ; i >= 0 ; i--) { // Checking if arr[i] is operationable if (arr[i] >= p) { // Performing operation of i-th element arr[i] = arr[i] - p; arr[i + 1 ] = arr[i + 1 ] + p; break ; } } // Decreasing the value of k by 1 k--; } // returning the rightmost element return arr[N - 1 ]; } // Driver Code public static void main(String[] args) { // Given Input int N = 4 , k = 5 , p = 2 ; int arr[] = { 3 , 8 , 1 , 4 }; // Function Call System.out.println( maxRightmostElement(N, k, p, arr)); } } // This code is contributed by dwivediyash |
Python3
# Python program for the above approach # Function to calculate maximum value of # Rightmost element def maxRightmostElement(N, k, p, arr): # Calculating maximum value of # Rightmost element while (k) : for i in range (N - 2 , - 1 , - 1 ) : # Checking if arr[i] is operationable if (arr[i] > = p) : # Performing operation of i-th element arr[i] = arr[i] - p arr[i + 1 ] = arr[i + 1 ] + p break # Decreasing the value of k by 1 k = k - 1 # Printing rightmost element print (arr[N - 1 ]) # Driver Code # Given Input N = 4 p = 2 k = 5 arr = [ 3 , 8 , 1 , 4 ] # Function Call maxRightmostElement(N, k, p, arr) # This code is contributed by sanjoy_62. |
C#
using System; public class GFG { // Function to calculate maximum value of // Rightmost element static int maxRightmostElement( int N, int k, int p, int []arr) { // Calculating maximum value of // Rightmost element while (k > 0) { for ( int i = N - 2; i >= 0; i--) { // Checking if arr[i] is operationable if (arr[i] >= p) { // Performing operation of i-th element arr[i] = arr[i] - p; arr[i + 1] = arr[i + 1] + p; break ; } } // Decreasing the value of k by 1 k--; } // returning the rightmost element return arr[N - 1]; } // Driver Code static public void Main() { // Given Input int N = 4, k = 5, p = 2; int [] arr = { 3, 8, 1, 4 }; // Function Call Console.WriteLine( maxRightmostElement(N, k, p, arr)); } } // This code is contributed by maddler. |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to calculate maximum value of // Rightmost element function maxRightmostElement(N, k, p, arr) { // Calculating maximum value of // Rightmost element while (k) { for (let i = N - 2; i >= 0; i--) { // Checking if arr[i] is operationable if (arr[i] >= p) { // Performing operation of i-th element arr[i] = arr[i] - p; arr[i + 1] = arr[i + 1] + p; break ; } } // Decreasing the value of k by 1 k--; } // Printing rightmost element document.write(arr[N - 1] + "<br>" ); } // Driver Code // Given Input let N = 4, p = 2, k = 5, arr = [3, 8, 1, 4]; // Function Call maxRightmostElement(N, k, p, arr); // This code is contributed by Potta Lokesh </script> |
8
Time Complexity: O(N*k)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by observing the fact that, i-th element of array arr[ ] can contribute 2 to the element arr[N-1] in N-1-i operations. follow the steps below to solve the problem:
- Iterate a loop, for i in range [N-2, 0].
- Initialize ans as arr[N-1] to store maximum value of rightmost element.
- Find minimum contribution of i-th element say d as, d = min(a[i]/2, k/(N-1-i)).
- Decrease k by d*(N-1-i) and increase ans by d*2.
- Print ans, it will be final step.
C++
// C++ program for above approach #include <iostream> using namespace std; // Function to calculate maximum value of // Rightmost element void maxRightmostElement( int N, int k, int arr[]){ // Initializing ans to store // Maximum valued rightmost element int ans = arr[N-1]; // Calculating maximum value of // Rightmost element for ( int i=N-2; i>=0; i--) { int d = min(arr[i]/2, k/(N-1-i)); k-=d*(N-1-i); ans+=d*2; } // Printing rightmost element cout<<ans<<endl; } // Driver Code int main() { // Given Input int N = 4, k = 5, arr[] = {3, 8, 1, 4}; // Function Call maxRightmostElement(N, k, arr); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG { // Function to calculate maximum value of // Rightmost element static int maxRightmostElement( int N, int k, int p, int arr[]) { // Initializing ans to store // Maximum valued rightmost element int ans = arr[N - 1 ]; // Calculating maximum value of // Rightmost element for ( int i = N - 2 ; i >= 0 ; i--) { int d = Math.min(arr[i] / p, k / (N - 1 - i)); k -= d * (N - 1 - i); ans += d * p; } // returning rightmost element return ans; } // Driver Code public static void main(String[] args) { // Given Input int N = 4 , k = 5 , p = 2 ; int arr[] = { 3 , 8 , 1 , 4 }; // Function Call System.out.println(maxRightmostElement(N, k, p, arr)); } } // This code is contributed by dwivediyash |
Python3
# Python 3 program for above approach # Function to calculate maximum value of # Rightmost element def maxRightmostElement(N, k, arr): # Initializing ans to store # Maximum valued rightmost element ans = arr[N - 1 ] # Calculating maximum value of # Rightmost element i = N - 2 while (i > = 0 ): d = min (arr[i] / / 2 , k / / (N - 1 - i)) k - = d * (N - 1 - i) ans + = d * 2 # Printing rightmost element i - = 1 print (ans,end = " " ) # Driver Code if __name__ = = '__main__' : # Given Input N = 4 k = 5 arr = [ 3 , 8 , 1 , 4 ] # Function Call maxRightmostElement(N, k, arr) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approach using System; public class GFG { // Function to calculate maximum value of // Rightmost element static int maxRightmostElement( int N, int k, int p, int []arr) { // Initializing ans to store // Maximum valued rightmost element int ans = arr[N - 1]; // Calculating maximum value of // Rightmost element for ( int i = N - 2; i >= 0; i--) { int d = Math.Min(arr[i] / p, k / (N - 1 - i)); k -= d * (N - 1 - i); ans += d * p; } // returning rightmost element return ans; } // Driver Code public static void Main( string [] args) { // Given Input int N = 4, k = 5, p = 2; int []arr = { 3, 8, 1, 4 }; // Function Call Console.WriteLine(maxRightmostElement(N, k, p, arr)); } } // This code is contributed by AnkThon |
Javascript
<script> // JavaScript program for the above approach // Function to calculate maximum value of // Rightmost element function maxRightmostElement( N, k, p, arr) { // Initializing ans to store // Maximum valued rightmost element var ans = arr[N - 1]; // Calculating maximum value of // Rightmost element for ( var i = N - 2; i >= 0; i--) { var d = Math.min(arr[i] / p, k / (N - 1 - i)); k -= d * (N - 1 - i); ans += d * p; } // returning rightmost element return ans; } // Driver Code // Given Input var N = 4, k = 3.5, p = 2; var arr = [ 3, 8, 1, 4 ]; // Function Call document.write(maxRightmostElement(N, k, p, arr)); // This code is contributed by shivanisinghss2110 </script> |
8
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!