Maximize the rightmost element of an array in k operations in Linear Time
Last Updated :
20 Oct, 2022
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++
#include <iostream>
using namespace std;
void maxRightmostElement( int N, int k, int p, int arr[]){
while (k)
{
for ( int i=N-2;i>=0;i--)
{
if (arr[i]>= p)
{
arr[i]=arr[i]-p;
arr[i+1]=arr[i+1]+p;
break ;
}
}
k--;
}
cout<<arr[N-1]<<endl;
}
int main() {
int N = 4, p = 2, k = 5, arr[] = {3, 8, 1, 4};
maxRightmostElement(N, k, p, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxRightmostElement( int N, int k, int p,
int arr[])
{
while (k > 0 ) {
for ( int i = N - 2 ; i >= 0 ; i--) {
if (arr[i] >= p)
{
arr[i] = arr[i] - p;
arr[i + 1 ] = arr[i + 1 ] + p;
break ;
}
}
k--;
}
return arr[N - 1 ];
}
public static void main(String[] args)
{
int N = 4 , k = 5 , p = 2 ;
int arr[] = { 3 , 8 , 1 , 4 };
System.out.println(
maxRightmostElement(N, k, p, arr));
}
}
|
Python3
def maxRightmostElement(N, k, p, arr):
while (k) :
for i in range (N - 2 , - 1 , - 1 ) :
if (arr[i] > = p) :
arr[i] = arr[i] - p
arr[i + 1 ] = arr[i + 1 ] + p
break
k = k - 1
print (arr[N - 1 ])
N = 4
p = 2
k = 5
arr = [ 3 , 8 , 1 , 4 ]
maxRightmostElement(N, k, p, arr)
|
C#
using System;
public class GFG {
static int maxRightmostElement( int N, int k, int p,
int []arr)
{
while (k > 0) {
for ( int i = N - 2; i >= 0; i--) {
if (arr[i] >= p) {
arr[i] = arr[i] - p;
arr[i + 1] = arr[i + 1] + p;
break ;
}
}
k--;
}
return arr[N - 1];
}
static public void Main()
{
int N = 4, k = 5, p = 2;
int [] arr = { 3, 8, 1, 4 };
Console.WriteLine(
maxRightmostElement(N, k, p, arr));
}
}
|
Javascript
<script>
function maxRightmostElement(N, k, p, arr) {
while (k) {
for (let i = N - 2; i >= 0; i--) {
if (arr[i] >= p) {
arr[i] = arr[i] - p;
arr[i + 1] = arr[i + 1] + p;
break ;
}
}
k--;
}
document.write(arr[N - 1] + "<br>" );
}
let N = 4, p = 2, k = 5, arr = [3, 8, 1, 4];
maxRightmostElement(N, k, p, arr);
</script>
|
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++
#include <iostream>
using namespace std;
void maxRightmostElement( int N, int k, int arr[]){
int ans = arr[N-1];
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;
}
cout<<ans<<endl;
}
int main() {
int N = 4, k = 5, arr[] = {3, 8, 1, 4};
maxRightmostElement(N, k, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxRightmostElement( int N, int k, int p, int arr[])
{
int ans = arr[N - 1 ];
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;
}
return ans;
}
public static void main(String[] args)
{
int N = 4 , k = 5 , p = 2 ;
int arr[] = { 3 , 8 , 1 , 4 };
System.out.println(maxRightmostElement(N, k, p, arr));
}
}
|
Python3
def maxRightmostElement(N, k, arr):
ans = arr[N - 1 ]
i = N - 2
while (i > = 0 ):
d = min (arr[i] / / 2 , k / / (N - 1 - i))
k - = d * (N - 1 - i)
ans + = d * 2
i - = 1
print (ans,end = " " )
if __name__ = = '__main__' :
N = 4
k = 5
arr = [ 3 , 8 , 1 , 4 ]
maxRightmostElement(N, k, arr)
|
C#
using System;
public class GFG {
static int maxRightmostElement( int N, int k, int p, int []arr)
{
int ans = arr[N - 1];
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;
}
return ans;
}
public static void Main( string [] args)
{
int N = 4, k = 5, p = 2;
int []arr = { 3, 8, 1, 4 };
Console.WriteLine(maxRightmostElement(N, k, p, arr));
}
}
|
Javascript
<script>
function maxRightmostElement( N, k, p, arr)
{
var ans = arr[N - 1];
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;
}
return ans;
}
var N = 4, k = 3.5, p = 2;
var arr = [ 3, 8, 1, 4 ];
document.write(maxRightmostElement(N, k, p, arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...