📌  相关文章
📜  N的最小值,使得从K到N的所有自然数的总和至少为X

📅  最后修改于: 2021-04-17 16:54:48             🧑  作者: Mango

给定两个正整数XK ,任务是找到N的最小值,以使[K,N]范围内的所有自然数之和至少为X。如果不存在N的可能值,则打印-1

例子:

天真的方法:解决此问题的最简单方法是检查[K,X]范围内的每个值,并从该范围返回第一个值,该值的前N个自然数之和至少为X。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum possible
// value of N such that sum of natural
// numbers from K to N is at least X
void minimumNumber(int K, int X)
{
    // If K is greater than X
    if (K > X) {
        cout << "-1";
        return;
    }
 
    // Stores value of minimum N
    int ans = 0;
 
    // Stores the sum of values
    // over the range [K, ans]
    int sum = 0;
 
    // Iterate over the range [K, N]
    for (int i = K; i <= X; i++) {
 
        sum += i;
 
        // Check if sum of first i
        // natural numbers is >= X
        if (sum >= X) {
            ans = i;
            break;
        }
    }
 
    // Print the possible value of ans
    cout << ans;
}
 
// Driver Code
int main()
{
    int K = 5, X = 13;
    minimumNumber(K, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the minimum possible
// value of N such that sum of natural
// numbers from K to N is at least X
static void minimumNumber(int K, int X)
{
     
    // If K is greater than X
    if (K > X)
    {
        System.out.println("-1");
        return;
    }
 
    // Stores value of minimum N
    int ans = 0;
 
    // Stores the sum of values
    // over the range [K, ans]
    int sum = 0;
 
    // Iterate over the range [K, N]
    for(int i = K; i <= X; i++)
    {
        sum += i;
 
        // Check if sum of first i
        // natural numbers is >= X
        if (sum >= X)
        {
            ans = i;
            break;
        }
    }
 
    // Print the possible value of ans
    System.out.println(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int K = 5, X = 13;
     
    minimumNumber(K, X);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to find the minimum possible
# value of N such that sum of natural
# numbers from K to N is at least X
def minimumNumber(K, X):
     
    # If K is greater than X
    if (K > X):
        print("-1")
        return
 
    # Stores value of minimum N
    ans = 0
 
    # Stores the sum of values
    # over the range [K, ans]
    sum = 0
 
    # Iterate over the range [K, N]
    for i in range(K, X + 1):
        sum += i
 
        # Check if sum of first i
        # natural numbers is >= X
        if (sum >= X):
            ans = i
            break
 
    # Print the possible value of ans
    print(ans)
 
# Driver Code
K = 5
X = 13
 
minimumNumber(K, X)
 
# This code is contributed by subham348


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum possible
// value of N such that sum of natural
// numbers from K to N is at least X
static void minimumNumber(int K, int X)
{
     
    // If K is greater than X
    if (K > X)
    {
        Console.Write("-1");
        return;
    }
 
    // Stores value of minimum N
    int ans = 0;
 
    // Stores the sum of values
    // over the range [K, ans]
    int sum = 0;
 
    // Iterate over the range [K, N]
    for(int i = K; i <= X; i++)
    {
        sum += i;
 
        // Check if sum of first i
        // natural numbers is >= X
        if (sum >= X)
        {
            ans = i;
            break;
        }
    }
 
    // Print the possible value of ans
    Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
    int K = 5, X = 13;
 
    minimumNumber(K, X);
}
}
 
// This code is contributed by sanjoy_62


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the sum of
// natural numbers from K to N is >= X
bool isGreaterEqual(int N, int K, int X)
{
    return ((N * 1LL * (N + 1) / 2)
            - ((K - 1) * 1LL * K / 2))
           >= X;
}
 
// Function to find the minimum value
// of N such that the sum of natural
// numbers from K to N is at least X
void minimumNumber(int K, int X)
{
    // If K is greater than X
    if (K > X) {
        cout << "-1";
        return;
    }
 
    int low = K, high = X, res = -1;
 
    // Perform the Binary Search
    while (low <= high) {
        int mid = low + (high - low) / 2;
 
        // If the sum of the natural
        // numbers from K to mid is atleast X
        if (isGreaterEqual(mid, K, X)) {
 
            // Update res
            res = mid;
 
            // Update high
            high = mid - 1;
        }
 
        // Otherwise, update low
        else
            low = mid + 1;
    }
 
    // Print the value of
    // res as the answer
    cout << res;
}
 
// Driver Code
int main()
{
    int K = 5, X = 13;
    minimumNumber(K, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if the sum of
// natural numbers from K to N is >= X
static boolean isGreaterEqual(int N, int K, int X)
{
    return ((N * 1L * (N + 1) / 2) -
           ((K - 1) * 1L * K / 2)) >= X;
}
 
// Function to find the minimum value
// of N such that the sum of natural
// numbers from K to N is at least X
static void minimumNumber(int K, int X)
{
     
    // If K is greater than X
    if (K > X)
    {
        System.out.println("-1");
        return;
    }
 
    int low = K, high = X, res = -1;
 
    // Perform the Binary Search
    while (low <= high)
    {
        int mid = low + (high - low) / 2;
 
        // If the sum of the natural
        // numbers from K to mid is atleast X
        if (isGreaterEqual(mid, K, X))
        {
             
            // Update res
            res = mid;
 
            // Update high
            high = mid - 1;
        }
 
        // Otherwise, update low
        else
            low = mid + 1;
    }
 
    // Print the value of
    // res as the answer
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int K = 5, X = 13;
     
    minimumNumber(K, X);
}
}
 
// This code is contributed by Kingash


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the sum of
// natural numbers from K to N is >= X
static bool isGreaterEqual(int N, int K, int X)
{
    return ((N * 1L * (N + 1) / 2) -
           ((K - 1) * 1L * K / 2)) >= X;
}
 
// Function to find the minimum value
// of N such that the sum of natural
// numbers from K to N is at least X
static void minimumNumber(int K, int X)
{
 
    // If K is greater than X
    if (K > X)
    {
        Console.Write("-1");
        return;
    }
 
    int low = K, high = X, res = -1;
 
    // Perform the Binary Search
    while (low <= high)
    {
        int mid = low + (high - low) / 2;
 
        // If the sum of the natural
        // numbers from K to mid is atleast X
        if (isGreaterEqual(mid, K, X))
        {
             
            // Update res
            res = mid;
 
            // Update high
            high = mid - 1;
        }
 
        // Otherwise, update low
        else
            low = mid + 1;
    }
 
    // Print the value of
    // res as the answer
    Console.WriteLine(res);
}
 
// Driver Code
public static void Main()
{
    int K = 5, X = 13;
 
    minimumNumber(K, X);
}
}
 
// This code is contributed by subham348


Javascript


输出:
7

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

高效的方法:可以通过使用二进制搜索来优化上述方法。请按照以下步骤解决给定的问题:

  • 初始化一个变量,例如res-1 ,以存储满足给定条件的N的最小可能值。
  • 初始化两个变量,KX ,并通过执行以下步骤在此范围上执行二进制搜索:
    • 找到中间低至+的值(高-低)/ 2。
    • 如果自然数从K中期之和大于或等于X或不。
    • 如果发现是正确的,则将res更新为mid并设置high =(mid – 1) 。否则,将低点更新为(mid + 1)
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the sum of
// natural numbers from K to N is >= X
bool isGreaterEqual(int N, int K, int X)
{
    return ((N * 1LL * (N + 1) / 2)
            - ((K - 1) * 1LL * K / 2))
           >= X;
}
 
// Function to find the minimum value
// of N such that the sum of natural
// numbers from K to N is at least X
void minimumNumber(int K, int X)
{
    // If K is greater than X
    if (K > X) {
        cout << "-1";
        return;
    }
 
    int low = K, high = X, res = -1;
 
    // Perform the Binary Search
    while (low <= high) {
        int mid = low + (high - low) / 2;
 
        // If the sum of the natural
        // numbers from K to mid is atleast X
        if (isGreaterEqual(mid, K, X)) {
 
            // Update res
            res = mid;
 
            // Update high
            high = mid - 1;
        }
 
        // Otherwise, update low
        else
            low = mid + 1;
    }
 
    // Print the value of
    // res as the answer
    cout << res;
}
 
// Driver Code
int main()
{
    int K = 5, X = 13;
    minimumNumber(K, X);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if the sum of
// natural numbers from K to N is >= X
static boolean isGreaterEqual(int N, int K, int X)
{
    return ((N * 1L * (N + 1) / 2) -
           ((K - 1) * 1L * K / 2)) >= X;
}
 
// Function to find the minimum value
// of N such that the sum of natural
// numbers from K to N is at least X
static void minimumNumber(int K, int X)
{
     
    // If K is greater than X
    if (K > X)
    {
        System.out.println("-1");
        return;
    }
 
    int low = K, high = X, res = -1;
 
    // Perform the Binary Search
    while (low <= high)
    {
        int mid = low + (high - low) / 2;
 
        // If the sum of the natural
        // numbers from K to mid is atleast X
        if (isGreaterEqual(mid, K, X))
        {
             
            // Update res
            res = mid;
 
            // Update high
            high = mid - 1;
        }
 
        // Otherwise, update low
        else
            low = mid + 1;
    }
 
    // Print the value of
    // res as the answer
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int K = 5, X = 13;
     
    minimumNumber(K, X);
}
}
 
// This code is contributed by Kingash

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the sum of
// natural numbers from K to N is >= X
static bool isGreaterEqual(int N, int K, int X)
{
    return ((N * 1L * (N + 1) / 2) -
           ((K - 1) * 1L * K / 2)) >= X;
}
 
// Function to find the minimum value
// of N such that the sum of natural
// numbers from K to N is at least X
static void minimumNumber(int K, int X)
{
 
    // If K is greater than X
    if (K > X)
    {
        Console.Write("-1");
        return;
    }
 
    int low = K, high = X, res = -1;
 
    // Perform the Binary Search
    while (low <= high)
    {
        int mid = low + (high - low) / 2;
 
        // If the sum of the natural
        // numbers from K to mid is atleast X
        if (isGreaterEqual(mid, K, X))
        {
             
            // Update res
            res = mid;
 
            // Update high
            high = mid - 1;
        }
 
        // Otherwise, update low
        else
            low = mid + 1;
    }
 
    // Print the value of
    // res as the answer
    Console.WriteLine(res);
}
 
// Driver Code
public static void Main()
{
    int K = 5, X = 13;
 
    minimumNumber(K, X);
}
}
 
// This code is contributed by subham348

Java脚本


输出:
7

时间复杂度: O(log(X – K))
辅助空间: O(1)