Maximize first array element by performing given operations at most K times
Given an array arr[] of size N an integer K, the task is to find the maximize the first element of the array by performing the following operations at most K times:
- Choose a pair of indices i and j (0 ? i, j ? N-1) such that |i ? j| = 1 and arri > 0.
- Set arri = arri ? 1 and arrj = arrj + 1 on those two indices.
Examples:
Input: arr[ ] = {1, 0, 3, 2}, K = 5
Output: 3
Explanation:
One of the possible set of operations can be:
Operation 1: Select i = 3 and j = 2. Therefore, the array modifies to {1, 1, 2, 2}.
Operation 2: Select i = 3 and j = 2. Therefore, the array modifies to {1, 2, 1, 2}.
Operation 3: Select i = 2 and j = 1. Therefore, the array modifies to {2, 1, 1, 2}.
Operation 4: Select i = 2 and j = 1. Therefore, the array modifies to {3, 0, 1, 2}.
Input: arr[] = {5, 1}, K = 2
Output: 6
Approach: Follow the steps below to solve the problem:
- At any point, it is optimal to choose indices i and j closest to first element of the array, with i > j.
- Therefore, for every operation, traverse the array from left to right and move the elements closer towards the first element.
- If all the elements are in the first position at some point, stop traversing and print the first array element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMax( int arr[], int N, int K)
{
for ( int i = 1; i < N; i++) {
int cur_val = arr[i];
while (K >= i) {
if (cur_val > 0) {
arr[0] = arr[0] + 1;
cur_val = cur_val - 1;
K = K - i;
}
else
break ;
}
}
cout << arr[0];
}
int main()
{
int arr[] = { 1, 0, 3, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 5;
getMax(arr, N, K);
return 0;
}
|
Java
class GFG{
static void getMax( int arr[], int N, int K)
{
for ( int i = 1 ; i < N; i++)
{
int cur_val = arr[i];
while (K >= i)
{
if (cur_val > 0 )
{
arr[ 0 ] = arr[ 0 ] + 1 ;
cur_val = cur_val - 1 ;
K = K - i;
}
else
break ;
}
}
System.out.print(arr[ 0 ]);
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 3 , 2 };
int N = arr.length;
int K = 5 ;
getMax(arr, N, K);
}
}
|
Python3
def getMax(arr, N, K):
for i in range ( 1 , N, 1 ):
cur_val = arr[i]
while (K > = i):
if (cur_val > 0 ):
arr[ 0 ] = arr[ 0 ] + 1
cur_val = cur_val - 1
K = K - i
else :
break
print (arr[ 0 ])
if __name__ = = '__main__' :
arr = [ 1 , 0 , 3 , 2 ]
N = len (arr)
K = 5
getMax(arr, N, K)
|
C#
using System;
class GFG{
static void getMax( int [] arr, int N,
int K)
{
for ( int i = 1; i < N; i++)
{
int cur_val = arr[i];
while (K >= i)
{
if (cur_val > 0)
{
arr[0] = arr[0] + 1;
cur_val = cur_val - 1;
K = K - i;
}
else
break ;
}
}
Console.Write(arr[0]);
}
static void Main()
{
int [] arr = { 1, 0, 3, 2 };
int N = arr.Length;
int K = 5;
getMax(arr, N, K);
}
}
|
Javascript
<script>
function getMax(arr , N , K) {
for (i = 1; i < N; i++) {
var cur_val = arr[i];
while (K >= i) {
if (cur_val > 0) {
arr[0] = arr[0] + 1;
cur_val = cur_val - 1;
K = K - i;
}
else
break ;
}
}
document.write(arr[0]);
}
var arr = [ 1, 0, 3, 2 ];
var N = arr.length;
var K = 5;
getMax(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...