给定两个正整数N和K ,任务是打印使总和等于K所需的最小数字计数,仅将前 N 个自然数中的任何元素相加一次。如果不可能得到等于K的总和,则打印“ -1 ”。
例子:
Input: N = 5, K = 10
Output: 3
Explanation:
The most optimal way is to choose number {1, 4, 5} to which will sum up to 10.
Therefore, print 3 which is the minimum count of elements needed.
Input: N = 5, K = 1000
Output: -1
Explanation:
It is impossible to get the sum equal to 1000.
方法:该问题可以使用贪心算法解决。请按照以下步骤解决此问题:
- 如果K大于 前N个自然数的和,然后打印“ -1 ”然后返回。
- 如果K小于等于N,则打印1然后返回。
- 否则,初始化变量 say sum ,并计数为0以存储所需数字的总和和最小计数。
- 迭代直到N大于等于1并且sum小于K并执行以下步骤:
- 将count增加1, sum增加N ,然后将N减少1 。
- 最后,如果以上情况都不满足,则打印计数作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum number of
// elements required to obtain sum K
int Minimum(int N, int K)
{
// Stores the maximum sum that
// can be obtained
int sum = N * (N + 1) / 2;
// If K is greater than the
// Maximum sum
if (K > sum)
return -1;
// If K is less than or equal
// to to N
if (K <= N)
return 1;
// Stores the sum
sum = 0;
// Stores the count of numbers
// needed
int count = 0;
// Iterate until N is greater than
// or equal to 1 and sum is less
// than K
while (N >= 1 && sum < K) {
// Increment count by 1
count += 1;
// Increment sum by N
sum += N;
// Update the sum
N -= 1;
}
// Finally, return the count
return count;
}
// Driver Code
int main()
{
// Given Input
int N = 5, K = 10;
// Function Call
cout << (Minimum(N, K));
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
import java.io.*;
// Function to find minimum number of
// elements required to obtain sum K
class GFG {
static int Minimum(int N, int K)
{
// Stores the maximum sum that
// can be obtained
int sum = N * (N + 1) / 2;
// If K is greater than the
// Maximum sum
if (K > sum)
return -1;
// If K is less than or equal
// to to N
if (K <= N)
return 1;
// Stores the sum
sum = 0;
// Stores the count of numbers
// needed
int count = 0;
// Iterate until N is greater than
// or equal to 1 and sum is less
// than K
while (N >= 1 && sum < K) {
// Increment count by 1
count += 1;
// Increment sum by N
sum += N;
// Update the sum
N -= 1;
}
// Finally, return the count
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 5, K = 10;
// Function Call
System.out.println(Minimum(N, K));
}
}
Python3
# Python3 program for the above approach
# Function to find minimum number of
# elements required to obtain sum K
def Minimum(N, K):
# Stores the maximum sum that
# can be obtained
sum = N * (N + 1) // 2
# If K is greater than the
# Maximum sum
if (K > sum):
return -1
# If K is less than or equal
# to to N
if (K <= N):
return 1
# Stores the sum
sum = 0
# Stores the count of numbers
# needed
count = 0
# Iterate until N is greater than
# or equal to 1 and sum is less
# than K
while (N >= 1 and sum < K):
# Increment count by 1
count += 1
# Increment sum by N
sum += N
# Update the sum
N -= 1
# Finally, return the count
return count
# Driver Code
if __name__ == '__main__':
# Given Input
N = 5
K = 10
# Function Call
print(Minimum(N, K))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
// Function to find minimum number of
// elements required to obtain sum K
class GFG{
static int Minimum(int N, int K)
{
// Stores the maximum sum that
// can be obtained
int sum = N * (N + 1) / 2;
// If K is greater than the
// Maximum sum
if (K > sum)
return -1;
// If K is less than or equal
// to to N
if (K <= N)
return 1;
// Stores the sum
sum = 0;
// Stores the count of numbers
// needed
int count = 0;
// Iterate until N is greater than
// or equal to 1 and sum is less
// than K
while (N >= 1 && sum < K)
{
// Increment count by 1
count += 1;
// Increment sum by N
sum += N;
// Update the sum
N -= 1;
}
// Finally, return the count
return count;
}
// Driver Code
static public void Main()
{
// Given Input
int N = 5, K = 10;
// Function Call
Console.Write(Minimum(N, K));
}
}
// This code is contributed by sanjoy_62
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(1)