📌  相关文章
📜  具有给定平均值的不同元素序列中的最大可能数量

📅  最后修改于: 2022-05-13 01:56:06.974000             🧑  作者: Mango

具有给定平均值的不同元素序列中的最大可能数量

给定整数NK ,如果序列元素的平均值为K ,任务是找到N长度的不同正整数序列可以具有的最大可能数。

例子:

方法:该问题的解决方案基于以下观察:

请按照以下步骤操作:

  • 计算给定序列的总和。
  • 求前(N-1) 个自然数之和。
  • 根据上述观察计算最大可能数。
  • 如果最大值小于 N,则不可能有这样的序列。
  • 否则,最大值为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate the
// maximum possible number in a sequence
// with given average and number of terms.
int maxNumber(int N, int K)
{
    // Sum of the sequence
    int sum = N * K;
 
    // Minimum possible sum of a sequence
    // having N-1 distinct positive integers
    int minSum = N * (N - 1) / 2;
 
    // Maximum number of the given sequence
    int maxNum = sum - minSum;
 
    // If such sequence is not possible
    if (maxNum < N)
        return -1;
   
    return maxNum;
}
 
// Driver Code
int main()
{
    int N = 5, K = 4;
    int maximum_number = maxNumber(N, K);
    cout << maximum_number;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
     
// Function to calculate the
// maximum possible number in a sequence
// with given average and number of terms.
static int maxNumber(int N, int K)
{
   
    // Sum of the sequence
    int sum = N * K;
 
    // Minimum possible sum of a sequence
    // having N-1 distinct positive integers
    int minSum = N * (N - 1) / 2;
 
    // Maximum number of the given sequence
    int maxNum = sum - minSum;
 
    // If such sequence is not possible
    if (maxNum < N)
        return -1;
   
    return maxNum;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 5, K = 4;
    int maximum_number = maxNumber(N, K);
    System.out.println(maximum_number);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
# Function to calculate the
# maximum possible number in a sequence
# with given average and number of terms.
def maxNumber(N, K):
 
    # Sum of the sequence
    sum = N * K;
 
    # Minimum possible sum of a sequence
    # having N-1 distinct positive integers
    minSum = (N * (N - 1) // 2);
 
    # Maximum number of the given sequence
    maxNum = sum - minSum;
 
    # If such sequence is not possible
    if (maxNum < N):
        return -1;
 
    return maxNum;
 
# Driver Code
N = 5
K = 4;
maximum_number = maxNumber(N, K);
print(maximum_number);
 
# This code is contributed by gfgking


C#
using System;
 
public class GFG{
 
  // Function to calculate the
  // maximum possible number in a sequence
  // with given average and number of terms.
  static int maxNumber(int N, int K)
  {
    // Sum of the sequence
    int sum = N * K;
 
    // Minimum possible sum of a sequence
    // having N-1 distinct positive integers
    int minSum = N * (N - 1) / 2;
 
    // Maximum number of the given sequence
    int maxNum = sum - minSum;
 
    // If such sequence is not possible
    if (maxNum < N)
      return -1;
 
    return maxNum;
  }
 
  // Driver Code
  static public void Main (){
 
    int N = 5, K = 4;
    int maximum_number = maxNumber(N, K);
    Console.Write(maximum_number);
  }
}
 
// This code is contributed by hrithikgarg03188


Javascript



输出
10

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