📜  求出N的最小值,使得前N个自然数的总和≥X

📅  最后修改于: 2021-04-17 17:45:50             🧑  作者: Mango

给定一个正整数X(1≤X≤10 6)中,任务是找到最小值N,使得第一N的自然数的总和为≥X。

例子:

简单方法:为了解决这个问题的最简单的方法是检查在范围[1,X]的每一个值,并从该范围内的量,第一N的自然数的总和被发现是≥X返回第一个值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Fucntion to check if sum of first
// N natural numbers is >= X
bool isGreaterEqual(int N, int X)
{
    return (N * 1LL * (N + 1) / 2) >= X;
}
 
// Finds minimum value of
// N such that sum of first
// N natural number >= X
int minimumPossible(int X)
{
    for (int i = 1; i <= X; i++) {
 
        // Check if sum of first i
        // natural number >= X
        if (isGreaterEqual(i, X))
            return i;
    }
}
 
// Driver Code
int main()
{
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    cout << minimumPossible(X);
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG
{
   
  // Fucntion to check if sum of first
  // N natural numbers is >= X
  static boolean isGreaterEqual(int N, int X)
  {
    return (N * (N + 1) / 2) >= X;
  }
 
  // Finds minimum value of
  // N such that sum of first
  // N natural number >= X
  static int minimumPossible(int X)
  {
    for (int i = 1; i <= X; i++)
    {
 
      // Check if sum of first i
      // natural number >= X
      if (isGreaterEqual(i, X))
        return i;
    }
    return 0;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
     
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    System.out.print(minimumPossible(X));
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 Program to implement
# the above approach
 
# Function to check if sum of first
# N natural numbers is >= X
def isGreaterEqual(N, X):
    return (N * (N + 1) // 2) >= X
 
# Finds minimum value of
# N such that sum of first
# N natural number >= X
def minimumPossible(X):
 
    for i in range(1, X + 1):
 
        # Check if sum of first i
        # natural number >= X
        if (isGreaterEqual(i, X)):
            return i
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    X = 14
 
    # Finds minimum value of
    # N such that sum of first
    # N natural number >= X
    print (minimumPossible(X))
 
    # This code is contributed by mohit kumar 29.


C#
// C# Program to implement
// the above approach
using System;
public class GFG
{
 
  // Fucntion to check if sum of first
  // N natural numbers is >= X
  static bool isGreaterEqual(int N, int X)
  {
    return (N * (N + 1) / 2) >= X;
  }
 
  // Finds minimum value of
  // N such that sum of first
  // N natural number >= X
  static int minimumPossible(int X)
  {
    for (int i = 1; i <= X; i++)
    {
 
      // Check if sum of first i
      // natural number >= X
      if (isGreaterEqual(i, X))
        return i;
    }
    return 0;
  }
 
  // Driver Code
  static public void Main ()
  {
 
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    Console.Write(minimumPossible(X));
  }
}
 
// This code is contributed by Dharanendra L V.


Javascript


C++
#include 
using namespace std;
 
// Function to check if sum of first
// N natural numbers is >= X
bool isGreaterEqual(int N, int X)
{
    return (N * 1LL * (N + 1) / 2) >= X;
}
 
// Finds minimum value of
// N such that sum of first
// N natural number >= X
int minimumPossible(int X)
{
 
    int low = 1, high = X, res = -1;
 
    // Binary Search
    while (low <= high) {
        int mid = low + (high - low) / 2;
 
        // Checks if sum of first 'mid' natural
        // numbers is greater than equal to X
        if (isGreaterEqual(mid, X)) {
            // Update res
            res = mid;
            // Update high
            high = mid - 1;
        }
        else
            // Update low
            low = mid + 1;
    }
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    cout << minimumPossible(X);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to check if sum of first
// N natural numbers is >= X
static boolean isGreaterEqual(int N, int X)
{
    return (N  * (N + 1) / 2) >= X;
}
 
// Finds minimum value of
// N such that sum of first
// N natural number >= X
static int minimumPossible(int X)
{
    int low = 1, high = X, res = -1;
 
    // Binary Search
    while (low <= high)
    {
        int mid = low + (high - low) / 2;
 
        // Checks if sum of first 'mid' natural
        // numbers is greater than equal to X
        if (isGreaterEqual(mid, X))
        {
           
            // Update res
            res = mid;
           
            // Update high
            high = mid - 1;
        }
        else
            // Update low
            low = mid + 1;
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    System.out.print( minimumPossible(X));
}
}
 
// This code is contributed by code_hunt.


Python3
# Function to check if sum of first
# N natural numbers is >= X
def isGreaterEqual(N, X):
    return (N * (N + 1) // 2) >= X;
 
# Finds minimum value of
# N such that sum of first
# N natural number >= X
def minimumPossible(X):
  low = 1
  high = X
  res = -1;
 
  # Binary Search
  while (low <= high):
        mid = low + (high - low) // 2;
 
        # Checks if sum of first 'mid' natural
        # numbers is greater than equal to X
        if (isGreaterEqual(mid, X)):
           
            # Update res
            res = mid;
             
            # Update high
            high = mid - 1;
 
        else:
            # Update low
            low = mid + 1;
 
  return res
 
# Driver Code
if __name__ == "__main__":
   
    # Input
    X = 14;
 
    # Finds minimum value of
    # N such that sum of first
    # N natural number >= X
    print(minimumPossible(X));
 
    # This code is contributed by chitranayal.


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function to check if sum of first
  // N natural numbers is >= X
  static bool isGreaterEqual(int N, int X)
  {
    return (N  * (N + 1) / 2) >= X;
  }
 
  // Finds minimum value of
  // N such that sum of first
  // N natural number >= X
  static int minimumPossible(int X)
  {
    int low = 1, high = X, res = -1;
 
    // Binary Search
    while (low <= high)
    {
      int mid = low + (high - low) / 2;
 
      // Checks if sum of first 'mid' natural
      // numbers is greater than equal to X
      if (isGreaterEqual(mid, X))
      {
 
        // Update res
        res = mid;
 
        // Update high
        high = mid - 1;
      }
      else
        // Update low
        low = mid + 1;
    }
    return res;
  }
 
 
  // Driver Code
  static public void Main()
  {
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    Console.Write( minimumPossible(X));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
5

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

高效方法:以下是上述方法的实现:

  1. 这个想法是使用二进制搜索来解决这个问题。
  2. 初始化变量low = 1,high = X并在此范围内执行二进制搜索。
  3. 计算位数=低+(高-低)/ 2,然后检查第一个中位数的总和是否大于或等于x。
  4. 如果sum≥X ,则将其存储在变量res中,并设置为high = mid-1
  5. 否则,设置低=中+ 1
  6. 打印res,这是必需的答案。

下面是上述方法的实现:

C++

#include 
using namespace std;
 
// Function to check if sum of first
// N natural numbers is >= X
bool isGreaterEqual(int N, int X)
{
    return (N * 1LL * (N + 1) / 2) >= X;
}
 
// Finds minimum value of
// N such that sum of first
// N natural number >= X
int minimumPossible(int X)
{
 
    int low = 1, high = X, res = -1;
 
    // Binary Search
    while (low <= high) {
        int mid = low + (high - low) / 2;
 
        // Checks if sum of first 'mid' natural
        // numbers is greater than equal to X
        if (isGreaterEqual(mid, X)) {
            // Update res
            res = mid;
            // Update high
            high = mid - 1;
        }
        else
            // Update low
            low = mid + 1;
    }
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    cout << minimumPossible(X);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to check if sum of first
// N natural numbers is >= X
static boolean isGreaterEqual(int N, int X)
{
    return (N  * (N + 1) / 2) >= X;
}
 
// Finds minimum value of
// N such that sum of first
// N natural number >= X
static int minimumPossible(int X)
{
    int low = 1, high = X, res = -1;
 
    // Binary Search
    while (low <= high)
    {
        int mid = low + (high - low) / 2;
 
        // Checks if sum of first 'mid' natural
        // numbers is greater than equal to X
        if (isGreaterEqual(mid, X))
        {
           
            // Update res
            res = mid;
           
            // Update high
            high = mid - 1;
        }
        else
            // Update low
            low = mid + 1;
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    System.out.print( minimumPossible(X));
}
}
 
// This code is contributed by code_hunt.

Python3

# Function to check if sum of first
# N natural numbers is >= X
def isGreaterEqual(N, X):
    return (N * (N + 1) // 2) >= X;
 
# Finds minimum value of
# N such that sum of first
# N natural number >= X
def minimumPossible(X):
  low = 1
  high = X
  res = -1;
 
  # Binary Search
  while (low <= high):
        mid = low + (high - low) // 2;
 
        # Checks if sum of first 'mid' natural
        # numbers is greater than equal to X
        if (isGreaterEqual(mid, X)):
           
            # Update res
            res = mid;
             
            # Update high
            high = mid - 1;
 
        else:
            # Update low
            low = mid + 1;
 
  return res
 
# Driver Code
if __name__ == "__main__":
   
    # Input
    X = 14;
 
    # Finds minimum value of
    # N such that sum of first
    # N natural number >= X
    print(minimumPossible(X));
 
    # This code is contributed by chitranayal.

C#

// C# program for the above approach
using System;
class GFG{
 
  // Function to check if sum of first
  // N natural numbers is >= X
  static bool isGreaterEqual(int N, int X)
  {
    return (N  * (N + 1) / 2) >= X;
  }
 
  // Finds minimum value of
  // N such that sum of first
  // N natural number >= X
  static int minimumPossible(int X)
  {
    int low = 1, high = X, res = -1;
 
    // Binary Search
    while (low <= high)
    {
      int mid = low + (high - low) / 2;
 
      // Checks if sum of first 'mid' natural
      // numbers is greater than equal to X
      if (isGreaterEqual(mid, X))
      {
 
        // Update res
        res = mid;
 
        // Update high
        high = mid - 1;
      }
      else
        // Update low
        low = mid + 1;
    }
    return res;
  }
 
 
  // Driver Code
  static public void Main()
  {
    // Input
    int X = 14;
 
    // Finds minimum value of
    // N such that sum of first
    // N natural number >= X
    Console.Write( minimumPossible(X));
  }
}
 
// This code is contributed by susmitakundugoaldanga.
输出:
5

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