Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 - GeeksforGeeks

28 Feb, 2022
Given a positive integer N, the task is to construct an array of length N and find the maximize the value at index K such that the sum of all the array elements is at most M and the absolute difference between any two consecutive array elements is at most 1.
Examples:
Input: N = 3, M = 7, K = 1Output: 3Explanation: According to the given constraints, the array with values {2, 3, 2}maximizes the value at index 1. Therefore, the required output is 3.
Input: N = 3, M = 8, K = 1Output: 3
Approach: The idea is to achieve the maximum value at index K and to decrease the sum of other elements to meet the criteria of the sum of the array to be at most M. Follow the steps below:
Let the value at index K be X. So the element at K – 1 is X – 1, at K – 2 is X – 2 and so on.
At index 0 the value is X – K. Similarly, at index K + 1 the value is X – 1 and so on upto X – (N – K – 1) at index N – 1.
So to achieve the maximum value at index K, the array structure would be X – K, X – (K – 1), ...., X – 2, X – 1, X, X – 1, X – 2, ....., X – (N – K – 1).
So after arranging the equation, it becomes K * X – (1 + 2 + .... + K) + X + (N – K – 1) * X – (1 + 2 + .... + (N – K – 1)) ≤ M.
Follow the steps to solve the above equation:
Calculate (1 + 2 + .... + K) using K * (K + 1) / 2 and (1 + 2 + ..... + (N – K – 1)) using (N – K – 1) * (N – K) / 2 and store in S1 and S2 respectively.
This reduces the equation to X * (K + 1 + N – K – 1) ≤ M + S1 + S2.
Now, the maximum value of X can be obtained by calculating (M + S1 + S2) / N.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to calculate the maximum// possible value at index Kvoid maxValueAtIndexK(int N, int K, int M){    // Stores the sum of elements    // in the left and right of index K    int S1 = 0, S2 = 0;     S1 = K * (K + 1) / 2;    S2 = (N - K - 1) * (N - K) / 2;     // Stores the maximum    // possible value at index K    int X = (M + S1 + S2) / N;     // Print the answer    cout << X;} // Driver Codeint main(){    // Given N, K & M    int N = 3, K = 1, M = 7;    maxValueAtIndexK(N, K, M);     return 0;}
// Java program for the above approachimport java.io.*; class GFG{ // Function to calculate the maximum// possible value at index Kstatic void maxValueAtIndexK(int N, int K,                             int M){         // Stores the sum of elements    // in the left and right of index K    int S1 = 0, S2 = 0;     S1 = K * (K + 1) / 2;    S2 = (N - K - 1) * (N - K) / 2;     // Stores the maximum    // possible value at index K    int X = (M + S1 + S2) / N;     // Print the answer    System.out.println(X);} // Driver Codepublic static void main(String[] args){         // Given N, K & M    int N = 3, K = 1, M = 7;         maxValueAtIndexK(N, K, M);}} // This code is contributed by Dharanendra L V
# Python program for the above approach # Function to calculate the maximum# possible value at index Kdef maxValueAtIndexK(N, K, M):     # Stores the sum of elements    # in the left and right of index K    S1 = 0; S2 = 0;    S1 = K * (K + 1) // 2;    S2 = (N - K - 1) * (N - K) // 2;     # Stores the maximum    # possible value at index K    X = (M + S1 + S2) // N;     # Print the answer    print(X); # Driver Codeif __name__ == '__main__':     # Given N, K & M    N = 3; K = 1; M = 7;    maxValueAtIndexK(N, K, M); # This code is contributed by 29AjayKumar
// C# program for the above approachusing System; class GFG{ // Function to calculate the maximum// possible value at index Kstatic void maxValueAtIndexK(int N, int K,                             int M){         // Stores the sum of elements    // in the left and right of index K    int S1 = 0, S2 = 0;     S1 = K * (K + 1) / 2;    S2 = (N - K - 1) * (N - K) / 2;     // Stores the maximum    // possible value at index K    int X = (M + S1 + S2) / N;     // Print the answer    Console.WriteLine(X);} // Driver Codestatic public void Main(){         // Given N, K & M    int N = 3, K = 1, M = 7;         maxValueAtIndexK(N, K, M);}} // This code is contributed by Dharanendra L V
<script>// JavaScript program for// the above approach  // Function to calculate the maximum// possible value at index Kfunction maxValueAtIndexK(N, K, M){    // Stores the sum of elements    // in the left and right of index K    let S1 = 0, S2 = 0;     S1 = K * (K + 1) / 2;    S2 = (N - K - 1) * (N - K) / 2;     // Stores the maximum    // possible value at index K    let X = (M + S1 + S2) / N;     // Print the answer     document.write(X);} // Driver Code      // Given N, K & M    let N = 3, K = 1, M = 7;    maxValueAtIndexK(N, K, M);  </script>
3
 
Time Complexity: O(1)Auxiliary Space: O(1)
 
dharanendralv23
29AjayKumar
avijitmondal1998
simranarora5sos
array-rearrange
Arrays
Greedy
Mathematical
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3
Coin Change | DP-7