具有给定平均值的不同元素序列中的最大可能数量
给定整数N和K ,如果序列元素的平均值为K ,任务是找到N长度的不同正整数序列可以具有的最大可能数。
例子:
Input: N = 4, K = 3
Output: 6
Explanation: The desired sequence would be – {1, 2, 3, 6}.
There could be other sequences as well satisfying the given values of N and K, such as {1, 2, 4, 5}.
But , since the maximum possible number in the sequence is needed, 6 would be the answer.
Input: N = 5, K = 4
Output: 10
Explanation: The desired sequence would be – {1, 2, 3, 4, 10}.
Input: N = 5, K = 2
Output: -1
Explanation: Forming a sequence having 5 distinct positive integer and average as 2 is not possible.
方法:该问题的解决方案基于以下观察:
- From the number of integers in the sequence and the average of the sequence, the total sum of all numbers of the sequence can be easily calculated by using the following formula:
sum of all numbers in sequence = (number of terms) x (average of all terms) - Now , suppose the maximum term (or number) is M and sum of the sequence is S. Then, to maximize M, (S-M) must be minimized.
- Since , the terms must be distinct and positive, so the series with minimum possible sum would be 1, 2, 3 . . . (N-1).
Sum of such a sequence of natural numbers can be easily calculated for (N-1) terms , and would be: (N * (N – 1)) / 2. - So , the maximum possible integer would be: M = sum of the given sequence – sum of first (N-1) natural numbers.
请按照以下步骤操作:
- 计算给定序列的总和。
- 求前(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)