Maximize first value by simultaneous increment and decrement of elements
Given an array nums[] of size N, Find the maximum value you can achieve at index 0 after performing the operation where in each operation increase the value at index 0 by 1 and decrease the value at index i by 1 such that nums[0] < nums[i] (1 ≤ i ≤ N-1). You can perform as many moves as you would like (possibly, zero).
Examples:
Input: nums[] = {1, 2, 3}
Output: 3
Explanation: nums[0] < nums[1], Therefore nums[0] + 1 and nums[1] – 1. Now, nums[0] = 2, nums[0] < nums[2], again repeat the step. Now nums[0] becomes 3 which is the maximum possible value we can get at index 0.Input: nums[] = {1, 2, 2}
Output: 2
Approach: The problem can be solved based on the following idea:
In order to achieve maximum value at index 0, sort the array from index 1 to the last index and can also start iterating it from index 1 and if at any point nums[i] is found to be greater than the element present at index 0, according to the given operation nums[0] get increased and nums[i] get decreased. So, nums[0] get increased by the amount (nums[i] – nums[0] + 1) / 2 when encountered with a greater value.
Follow the steps mentioned below to implement the idea:
- Store the initial value present at index 0 in a variable.
- Sort the array from index 1 to the last index.
- Iterate from index 1 and if found nums[i] > nums[0], do the given operation
- Value at index 0 will get increased by the (nums[i] – nums[0] +1) / 2
- Return the value at index 0 as the required answer.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function for finding out maximum value // at index 0 int maximumValue(vector< int >& nums, int n) { // Initial value at index 0 int value_at_index_0 = nums[0]; // Sorting the array from index 1 to // last index sort(nums.begin() + 1, nums.end()); // Iterating array from index 1 for ( int i = 1; i < n; i++) { // If found greater element than the // value present at index 0, do the // given operation if (nums[i] > value_at_index_0) { // Value at index 0 get increased cout<<(nums[i] - value_at_index_0 + 1) / 2<< " " ; value_at_index_0 += (nums[i] - value_at_index_0 + 1) / 2; } } // Returning the maximum value return value_at_index_0; } // Driver code int main() { vector< int > nums = { 1, 2, 3 }; int N = nums.size(); // Function call cout << maximumValue(nums, N); return 0; } |
Python3
#Python code for the above approach # Function for finding out maximum value # at index 0 def maximumValue(nums, n): # Initial value at index 0 value_at_index_0 = nums[ 0 ] # Sorting the array from index 1 to # last index nums.sort() # Iterating array from index 1 for i in range ( 1 , n): # If found greater element than the # value present at index 0, do the # given operation if nums[i] > value_at_index_0: # Value at index 0 get increased value_at_index_0 + = (nums[i] - value_at_index_0 + 1 ) / / 2 # Returning the maximum value return value_at_index_0 # Driver code if __name__ = = '__main__' : nums = [ 1 , 2 , 3 ] N = len (nums) # Function call print (maximumValue(nums, N)) #This code is contributed by Potta Lokesh |
Javascript
// Javascript code to implement the approach // Function for finding out maximum value // at index 0 function maximumValue(nums, n) { // Initial value at index 0 let value_at_index_0 = nums[0]; // Sorting the array from index 1 to // last index nums = nums.slice(1, nums.length).sort( function (a, b){ return a - b;}); nums.unshift(value_at_index_0); // Iterating array from index 1 for (let i = 1; i < n; i++) { // If found greater element than the // value present at index 0, do the // given operation if (nums[i] > value_at_index_0) { // Value at index 0 get increased value_at_index_0 += Math.floor((nums[i] - value_at_index_0 + 1) / 2); } } // Returning the maximum value return value_at_index_0; } // Driver code let nums = [1, 2, 3 ]; let N = nums.length; // Function call console.log(maximumValue(nums, N)); // This code is contributed by agrawalpoojaa976. |
Java
import java.util.*; public class Main { public static void main(String[] args) { int [] nums = { 1 , 2 , 3 }; int N = nums.length; System.out.println(maximumValue(nums, N)); } // Function for finding out maximum value // at index 0 public static int maximumValue( int [] nums, int n) { // Initial value at index 0 int value_at_index_0 = nums[ 0 ]; // Sorting the array from index 1 to // last index Arrays.sort(nums, 1 , n); // Iterating array from index 1 for ( int i = 1 ; i < n; i++) { // If found greater element than the // value present at index 0, do the // given operation if (nums[i] > value_at_index_0) { // Value at index 0 get increased System.out.print((nums[i] - value_at_index_0 + 1 ) / 2 + " " ); value_at_index_0 += (nums[i] - value_at_index_0 + 1 ) / 2 ; } } // Returning the maximum value return value_at_index_0; } } |
C#
// C# code to implement the approach using System; public class GFG { // Function for finding out maximum value // at index 0 static int maximumValue( int [] nums, int n) { // Initial value at index 0 int value_at_index_0 = nums[0]; // Sorting the array from index 1 to // last index Array.Sort(nums, 1, nums.Length - 1); // Iterating array from index 1 for ( int i = 1; i < n; i++) { // If found greater element than the // value present at index 0, do the // given operation if (nums[i] > value_at_index_0) { // Value at index 0 get increased Console.Write( ((nums[i] - value_at_index_0 + 1) / 2) + " " ); value_at_index_0 += (nums[i] - value_at_index_0 + 1) / 2; } } // Returning the maximum value return value_at_index_0; } // Driver code public static void Main() { int [] nums = { 1, 2, 3 }; int N = nums.Length; // Function call Console.Write(maximumValue(nums, N)); } } |
3
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Related Articles:
Please Login to comment...