Open In App

Maximize first array element by performing given operations at most K times

Last Updated : 20 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. Choose a pair of indices i and j (0 ? i, j ? N-1) such that |i ? j| = 1 and arri > 0.
  2. 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:

  1. At any point, it is optimal to choose indices i and j closest to first element of the array, with i > j.
  2. Therefore, for every operation, traverse the array from left to right and move the elements closer towards the first element.
  3. 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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to maximize
// the first array element
int getMax(int arr[], int N, int K)
{
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        // Initialize cur_val to a[i]
        int cur_val = arr[i];
 
        // If all operations
        // are not over yet
        while (K >= i) {
 
            // If current value is
            // greater than zero
            if (cur_val > 0) {
 
                // Incrementing first
                // element of array by 1
                arr[0] = arr[0] + 1;
 
                // Decrementing current
                // value of array by 1
                cur_val = cur_val - 1;
 
                // Decrementing number
                // of operations by i
                K = K - i;
            }
 
            // If current value is
            // zero, then break
            else
                break;
        }
    }
 
    // Print first array element
    cout << arr[0];
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 0, 3, 2 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given K
    int K = 5;
 
    // Prints the maximum
    // possible value of the
    // first array element
    getMax(arr, N, K);
 
    return 0;
}


Java




// Java program for the above approach
class GFG{
 
  // Function to maximize
  // the first array element
  static void getMax(int arr[], int N, int K)
  {
 
    // Traverse the array
    for (int i = 1; i < N; i++)
    {
 
      // Initialize cur_val to a[i]
      int cur_val = arr[i];
 
      // If all operations
      // are not over yet
      while (K >= i)
      {
 
        // If current value is
        // greater than zero
        if (cur_val > 0)
        {
 
          // Incrementing first
          // element of array by 1
          arr[0] = arr[0] + 1;
 
          // Decrementing current
          // value of array by 1
          cur_val = cur_val - 1;
 
          // Decrementing number
          // of operations by i
          K = K - i;
        }
 
        // If current value is
        // zero, then break
        else
          break;
      }
    }
 
    // Print first array element
    System.out.print(arr[0]);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given array
    int arr[] = { 1, 0, 3, 2 };
 
    // Size of the array
    int N = arr.length;
 
    // Given K
    int K = 5;
 
    // Prints the maximum
    // possible value of the
    // first array element
    getMax(arr, N, K);
  }
}
 
// This code is contributed by shikhasingrajput


Python3




# Python3 program for the above approach
 
# Function to maximize
# the first array element
def getMax(arr, N, K):
     
    # Traverse the array
    for i in range(1, N, 1):
         
        # Initialize cur_val to a[i]
        cur_val = arr[i]
 
        # If all operations
        # are not over yet
        while (K >= i):
             
            # If current value is
            # greater than zero
            if (cur_val > 0):
 
                # Incrementing first
                # element of array by 1
                arr[0] = arr[0] + 1
 
                # Decrementing current
                # value of array by 1
                cur_val = cur_val - 1
 
                # Decrementing number
                # of operations by i
                K = K - i
 
            # If current value is
            # zero, then break
            else:
                break
 
    # Print first array element
    print(arr[0])
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 1, 0, 3, 2 ]
 
    # Size of the array
    N = len(arr)
 
    # Given K
    K = 5
 
    # Prints the maximum
    # possible value of the
    # first array element
    getMax(arr, N, K)
 
# This code is contributed by SURENDRA_GANGWAR


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to maximize
// the first array element
static void getMax(int[] arr, int N,
                   int K)
{
     
    // Traverse the array
    for(int i = 1; i < N; i++)
    {
         
        // Initialize cur_val to a[i]
        int cur_val = arr[i];
   
        // If all operations
        // are not over yet
        while (K >= i)
        {
             
            // If current value is
            // greater than zero
            if (cur_val > 0)
            {
                 
                // Incrementing first
                // element of array by 1
                arr[0] = arr[0] + 1;
                 
                // Decrementing current
                // value of array by 1
                cur_val = cur_val - 1;
                 
                // Decrementing number
                // of operations by i
                K = K - i;
            }
   
            // If current value is
            // zero, then break
            else
                break;
        }
    }
     
    // Print first array element
    Console.Write(arr[0]);
 
// Driver code
static void Main()
{
     
    // Given array
    int[] arr = { 1, 0, 3, 2 };
     
    // Size of the array
    int N = arr.Length;
     
    // Given K
    int K = 5;
     
    // Prints the maximum
    // possible value of the
    // first array element
    getMax(arr, N, K);
}
}
 
// This code is contributed by divyesh072019


Javascript




<script>
// javascript program for the above approach   
// Function to maximize
    // the first array element
    function getMax(arr , N , K) {
 
        // Traverse the array
        for (i = 1; i < N; i++) {
 
            // Initialize cur_val to a[i]
            var cur_val = arr[i];
 
            // If all operations
            // are not over yet
            while (K >= i) {
 
                // If current value is
                // greater than zero
                if (cur_val > 0) {
 
                    // Incrementing first
                    // element of array by 1
                    arr[0] = arr[0] + 1;
 
                    // Decrementing current
                    // value of array by 1
                    cur_val = cur_val - 1;
 
                    // Decrementing number
                    // of operations by i
                    K = K - i;
                }
 
                // If current value is
                // zero, then break
                else
                    break;
            }
        }
 
        // Print first array element
        document.write(arr[0]);
    }
 
    // Driver Code
     
        // Given array
        var arr = [ 1, 0, 3, 2 ];
 
        // Size of the array
        var N = arr.length;
 
        // Given K
        var K = 5;
 
        // Prints the maximum
        // possible value of the
        // first array element
        getMax(arr, N, K);
 
// This code is contributed by aashish1995
</script>


Output: 

3

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Similar Reads

Maximize the minimum value of Array by performing given operations at most K times
Given array A[] of size N and integer K, the task for this problem is to maximize the minimum value of the array by performing given operations at most K times. In one operation choose any index and increase that array element by 1. Examples: Input: A[] = {3, 1, 2, 4, 6, 2, 5}, K = 8Output: 4Explanation: Choose i = 1 and increase the element by 1 n
10 min read
Maximum inversions in a sequence of 1 to N after performing given operations at most K times
Given two integers N and K, the task is to find the maximum number of inversion in a sequence of first N natural numbers after performing at max K operations. In each operation, any two elements of the sequence can be swapped. Note: elements of the sequence are arranged in ascending order and there are no repeated elements in the sequence.Example:
7 min read
Construct array B as last element left of every suffix array obtained by performing given operations on every suffix of given array
Given an array arr[] of N integers, the task is to print the last element left of every suffix array obtained by performing the following operation on every suffix of the array, arr[]: Copy the elements of the suffix array into an array suff[].Update ith suffix element as suff[i] = (suff[i] OR suff[i+1]) - (suff[i] XOR suff[i+1]) reducing the size
9 min read
Maximize length of subarray of equal elements by performing at most K increment operations
Given an array A[] consisting of N integers and an integer K, the task is to maximize the length of the subarray having equal elements after performing at most K increments by 1 on array elements. Note: Same array element can be incremented more than once. Examples: Input: A[] = {2, 4, 8, 5, 9, 6}, K = 6Output: 3Explanation:Subarray [8, 5, 9] can b
8 min read
Make the array elements equal by performing given operations minimum number of times
Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: arr[] = {99, 96, 97, 95}Output: 3Explanation:Operat
7 min read
Find the index of the array elements after performing given operations K times
Given an array arr[] and an integer K, the task is to print the position of the array elements, where the ith value in the result is the index of the ith element in the original array after applying following operations exactly K times: Remove the first array element and decrement it by 1.If it is greater than 0 after decrementing, place it at the
7 min read
Maximize Kth largest element after splitting the given Array at most C times
Given an array arr[] and two positive integers K and C, the task is to maximize the Kth maximum element obtained after splitting an array element arr[] into two parts(not necessarily an integer) C number of times. Print -1 if there doesn't exist Kth maximum element. Note: It is compulsory to do splitting operation until the size of the array arr[]
7 min read
Maximize sum of array elements removed by performing the given operations
Given two arrays arr[] and min[] consisting of N integers and an integer K. For each index i, arr[i] can be reduced to at most min[i]. Consider a variable, say S(initially 0). The task is to find the maximum value of S that can be obtained by performing the following operations: Choose an index i and add max(arr[i], min[i]) to S.Remove the chosen e
8 min read
Find lexicographical smallest string by performing the given operations N times
Given a string S of N characters, the task is to find the smallest lexicographical string after performing each of the following operations N times in any order: Remove the 1st character of S and insert it into a stack X.Remove the top of stack X and append it to the end of another string Y which is initially empty. Example: Input: S = "cab"Output:
8 min read
Maximize first element of Array by deleting first or adding a previously deleted element
Given an array arr[] of size N, and an integer K, the task is to maximize the first element of the array in K operations where in each operation: If the array is not empty, remove the topmost element of the array.Add any one of the previously removed element back at the starting of the array. Examples: Input: arr[] = [5, 2, 2, 4, 0, 6], K = 4Output
7 min read