📌  相关文章
📜  最大化总和为M且相邻元素之间的差最大为1的数组中索引K处的元素最大化

📅  最后修改于: 2021-05-14 02:40:18             🧑  作者: Mango

给定一个正整数N ,任务是构造一个长度为N的数组,并找到索引K处的值的最大值,以使所有数组元素的总和至多为M且任何两个连续数组元素之间的绝对差为。大多数1

例子:

方法:想法是在索引K处获得最大值,并减少其他元素的总和以满足数组总和最大为M的标准。请按照以下步骤操作:

  • 令索引K的值为X。因此,在K – 1处的元素是X – 1 ,在K – 2处的元素是X – 2 ,依此类推。
  • 在索引0处,值为X – K。同样,在索引K + 1处,值是X – 1 ,以此类推,直到索引N – 1处的值是X –(N – K – 1)
  • 因此,要在索引K处获得最大值,数组结构应为X – K,X –(K – 1),…。,X – 2,X – 1,X,X – 1,X – 2…。 。,X –(N – K – 1)
  • 因此,在安排了等式之后,它变为K * X –(1 + 2 +…。+ K)+ X +(N – K – 1)* X –(1 + 2 +…。+(N – K – 1) )≤M

请按照下列步骤来解决上述方程式:

  • 使用(K +(K + 1)/ 2(1 + 2 +….. +(N – K – 1)))使用(N – K – 1)*(N来计算(1 + 2 +…。+ K) – K)/ 2并分别存储在S1S2中
  • 这将等式简化为X *(K +1 + N – K – 1)≤M + S1 + S2
  • 现在,可以通过计算(M + S1 + S2)/ N来获得X的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


Python3
# 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;
 
    # Prthe 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


输出:
3

时间复杂度: O(1)
辅助空间: O(1)