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

📅  最后修改于: 2021-09-03 13:47:56             🧑  作者: Mango

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

例子:

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

  • 设索引K处的值为X 。所以K – 1处的元素是X – 1K – 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

按照以下步骤求解上述方程:

  • 计算(1 + 2 + …. + K)使用K * (K + 1) / 2(1 + 2 + ….. + (N – K – 1))使用(N – K – 1) * (N – 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live