Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 Last Updated : 14 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a positive integer N, the task is to construct an array of length N and find the maximum 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: 3Approach: 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++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate the maximum // possible value at index K 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 cout << X; } // Driver Code int main() { // Given N, K & M int N = 3, K = 1, M = 7; maxValueAtIndexK(N, K, M); return 0; } Java // Java program for the above approach import java.io.*; class GFG{ // Function to calculate the maximum // possible value at index K static 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 Code public 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 # Python program for the above approach # Function to calculate the maximum # possible value at index K def 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 Code if __name__ == '__main__': # Given N, K & M N = 3; K = 1; M = 7; maxValueAtIndexK(N, K, M); # This code is contributed by 29AjayKumar C# // C# program for the above approach using System; class GFG{ // Function to calculate the maximum // possible value at index K static 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 Code static 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 JavaScript <script> // JavaScript program for // the above approach // Function to calculate the maximum // possible value at index K function 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> Output3Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Smallest number that can replace all -1s in an array such that maximum absolute difference between any pair of adjacent elements is minimum P promaroy20 Follow Improve Article Tags : Greedy Mathematical DSA Arrays array-rearrange +1 More Practice Tags : ArraysGreedyMathematical Similar Reads Maximize sum of absolute difference between adjacent elements in Array with sum K Given two integers N and K, the task is to maximize the sum of absolute differences between adjacent elements of an array of length N and sum K. Examples: Input: N = 5, K = 10 Output: 20 Explanation: The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adj 4 min read Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 Given an array arr[] consisting of N positive integers, the task is to maximize the sum of the array element such that the first element of the array is 1 and the difference between the adjacent elements of the array is at most 1 after performing the following operations: Rearrange the array element 7 min read Maximize value at Kth index to create N size array with adjacent difference 1 and sum less than M Given three numbers N, K, and M, the task is to find the maximum value that can be assigned to the Kth index if positive values are assigned to all N indices such that the total sum of values is less than M, and the difference between the values at adjacent positions is atmost 1. Examples: Input : N 11 min read Maximize the minimum difference between any element pair by selecting K elements from given Array Given an array of N integers the task is to select K elements out of these N elements in such a way that the minimum difference between each of the K numbers is the Largest. Return the largest minimum difference after choosing any K elements. Examples: Input: N = 4, K = 3, arr = [2, 6, 2, 5]Output: 11 min read Smallest number that can replace all -1s in an array such that maximum absolute difference between any pair of adjacent elements is minimum Given an array arr[] consisting of N positive integers and some elements as -1, the task is to find the smallest number, say K, such that replacing all -1s in the array by K minimizes the maximum absolute difference between any pair of adjacent elements. Examples: Input: arr[] = {-1, 10, -1, 12, -1} 8 min read Maximum sum subsequence made up of at most K distant elements including the first and last array elements Given an array arr[] of size n, and an integer k. The task is to find the maximum sum of a subsequence that satisfies the following two conditions:The subsequence must include both arr[0] and arr[n - 1].The indices of consecutive elements chosen in the subsequence must differ by at most k.Examples:I 12 min read Like