📌  相关文章
📜  最大化 X 使得 [1, X] 范围内的数字总和最多为 K

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

最大化 X 使得 [1, X] 范围内的数字总和最多为 K

给定两个整数N和一个整数K ,任务是找到小于或等于N的整数的计数,使得直到该整数的自然数之和小于或等于K

例子:

方法:最简单的方法是遍历范围[1, N]并计算总和小于或等于K的元素的数量。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
 
int Count(int N, int K)
{
    // If K equals to 0
    if (K == 0)
        return 0;
 
    // Stores sum of first i natural
    // numbers
    int sum = 0;
 
    // Stores the result
    int res = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
        // Increment sum by i
        sum += i;
 
        // Is sum is less than or
        // equal to K
        if (sum <= K)
            res++;
 
        // Otherwise,
        else
            break;
    }
    // Return res
    return res;
}
// Driver Code
int main()
{
    // Input
    int N = 6, K = 14;
 
    // Function call
    cout << Count(N, K);
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
   
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
static int Count(int N, int K)
{
    // If K equals to 0
    if (K == 0)
        return 0;
 
    // Stores sum of first i natural
    // numbers
    int sum = 0;
 
    // Stores the result
    int res = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++)
    {
        // Increment sum by i
        sum += i;
 
        // Is sum is less than or
        // equal to K
        if (sum <= K)
            res++;
 
        // Otherwise,
        else
            break;
    }
   
    // Return res
    return res;
}
   
// Driver Code
 public static void main(String args[])
{
    // Input
    int N = 6, K = 14;
 
    // Function call
    System.out.println(Count(N, K));
    }
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
 
# Function to count the elements with
# sum of the first that many natural
# numbers less than or equal to K
def Count(N, K):
     
    # If K equals to 0
    if (K == 0):
        return 0
 
    # Stores sum of first i natural
    # numbers
    sum = 0
 
    # Stores the result
    res = 0
 
    # Iterate over the range [1, N]
    for i in range(1, N + 1, 1):
         
        # Increment sum by i
        sum += i
 
        # Is sum is less than or
        # equal to K
        if (sum <= K):
            res += 1
 
        # Otherwise,
        else:
            break
 
    # Return res
    return res
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    N = 6
    K = 14
 
    # Function call
    print(Count(N, K))
 
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to count the elements with
    // sum of the first that many natural
    // numbers less than or equal to K
    static int Count(int N, int K)
    {
        // If K equals to 0
        if (K == 0)
            return 0;
 
        // Stores sum of first i natural
        // numbers
        int sum = 0;
 
        // Stores the result
        int res = 0;
 
        // Iterate over the range [1, N]
        for (int i = 1; i <= N; i++) {
            // Increment sum by i
            sum += i;
 
            // Is sum is less than or
            // equal to K
            if (sum <= K)
                res++;
 
            // Otherwise,
            else
                break;
        }
 
        // Return res
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        // Input
        int N = 6, K = 14;
 
        // Function call
        Console.WriteLine(Count(N, K));
    }
}
 
// This code is contributed by subhammahato348.


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
 
int Count(int N, int K)
{
    // If K equals to 0
    if (K == 0)
        return 0;
 
    // Stores the result
    int res = 0;
 
    int low = 1, high = N;
 
    // Iterate until low is less than
    // or equal to high
    while (low <= high) {
 
        int mid = (low + high) / 2;
 
        // Stores the sum of first mid
        // natural numbers
 
        int sum = (mid * mid + mid) / 2;
        // If sum is less than or equal
        // to K
        if (sum <= K) {
            // Update res and low
            res = max(res, mid);
            low = mid + 1;
        }
        // Otherwise,
        else {
            // Update
            high = mid - 1;
        }
    }
 
    // Return res
    return res;
}
// Driver Code
int main()
{
    // Input
    int N = 6, K = 14;
 
    // Function call
    cout << Count(N, K);
    return 0;
}


Java
// Java program for above approach
import java.util.*;
class GFG{
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
    static int Count(int N, int K)
    {
       
        // If K equals to 0
        if (K == 0)
            return 0;
 
        // Stores the result
        int res = 0;
 
        int low = 1, high = N;
 
        // Iterate until low is less than
        // or equal to high
        while (low <= high) {
 
            int mid = (low + high) / 2;
 
            // Stores the sum of first mid
            // natural numbers
 
            int sum = (mid * mid + mid) / 2;
            // If sum is less than or equal
            // to K
            if (sum <= K)
            {
               
                // Update res and low
                res = Math.max(res, mid);
                low = mid + 1;
            }
           
            // Otherwise,
            else
            {
               
                // Update
                high = mid - 1;
            }
        }
 
        // Return res
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Input
        int N = 6, K = 14;
 
        // Function call
        System.out.println(Count(N, K));
    }
}
 
// This code is contributed by hritikrommie.


Python3
# Python3 program for the above approach
 
# Function to count the elements with
# sum of the first that many natural
# numbers less than or equal to K
def Count(N, K):
     
    # If K equals to 0
    if (K == 0):
        return 0
 
    # Stores the result
    res = 0
 
    low = 1
    high = N
 
    # Iterate until low is less than
    # or equal to high
    while (low <= high):
        mid = (low + high) // 2
 
        # Stores the sum of first mid
        # natural numbers
        sum = (mid * mid + mid) // 2
         
        # If sum is less than or equal
        # to K
        if (sum <= K):
             
            # Update res and low
            res = max(res, mid)
            low = mid + 1
         
        # Otherwise,
        else:
             
            # Update
            high = mid - 1
             
    # Return res
    return res
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    N = 6
    K = 14
 
    # Function call
    print(Count(N, K))
     
# This code is contributed by shivanisinghss2110


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
static int Count(int N, int K)
{
     
    // If K equals to 0
    if (K == 0)
        return 0;
 
    // Stores the result
    int res = 0;
 
    int low = 1, high = N;
 
    // Iterate until low is less than
    // or equal to high
    while (low <= high)
    {
        int mid = (low + high) / 2;
 
        // Stores the sum of first mid
        // natural numbers
 
        int sum = (mid * mid + mid) / 2;
         
        // If sum is less than or equal
        // to K
        if (sum <= K)
        {
             
            // Update res and low
            res = Math.Max(res, mid);
            low = mid + 1;
        }
       
        // Otherwise,
        else
        {
             
            // Update
            high = mid - 1;
        }
    }
 
    // Return res
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Input
    int N = 6, K = 14;
 
    // Function call
    Console.Write (Count(N, K));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
4

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

高效方法:上述方法可以通过使用二分搜索算法进行优化。请按照以下步骤解决问题:

  • 初始化一个变量res0来存储结果。
  • 此外,将两个变量(例如lowhigh )分别初始化为1N。
  • 迭代直到low小于或等于high并执行以下步骤:
    • 找到范围[low, high]的中间值并将其存储在一个变量中,比如mid
    • 计算到mid的自然数之和,并将其存储在一个变量中,比如sum
    • 如果总和小于或等于K ,则将res更新为max(res, mid)并将mid+1分配给low
    • 否则,将mid-1分配给high
  • 最后,完成上述步骤后,打印res的值作为答案。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
 
int Count(int N, int K)
{
    // If K equals to 0
    if (K == 0)
        return 0;
 
    // Stores the result
    int res = 0;
 
    int low = 1, high = N;
 
    // Iterate until low is less than
    // or equal to high
    while (low <= high) {
 
        int mid = (low + high) / 2;
 
        // Stores the sum of first mid
        // natural numbers
 
        int sum = (mid * mid + mid) / 2;
        // If sum is less than or equal
        // to K
        if (sum <= K) {
            // Update res and low
            res = max(res, mid);
            low = mid + 1;
        }
        // Otherwise,
        else {
            // Update
            high = mid - 1;
        }
    }
 
    // Return res
    return res;
}
// Driver Code
int main()
{
    // Input
    int N = 6, K = 14;
 
    // Function call
    cout << Count(N, K);
    return 0;
}

Java

// Java program for above approach
import java.util.*;
class GFG{
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
    static int Count(int N, int K)
    {
       
        // If K equals to 0
        if (K == 0)
            return 0;
 
        // Stores the result
        int res = 0;
 
        int low = 1, high = N;
 
        // Iterate until low is less than
        // or equal to high
        while (low <= high) {
 
            int mid = (low + high) / 2;
 
            // Stores the sum of first mid
            // natural numbers
 
            int sum = (mid * mid + mid) / 2;
            // If sum is less than or equal
            // to K
            if (sum <= K)
            {
               
                // Update res and low
                res = Math.max(res, mid);
                low = mid + 1;
            }
           
            // Otherwise,
            else
            {
               
                // Update
                high = mid - 1;
            }
        }
 
        // Return res
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Input
        int N = 6, K = 14;
 
        // Function call
        System.out.println(Count(N, K));
    }
}
 
// This code is contributed by hritikrommie.

Python3

# Python3 program for the above approach
 
# Function to count the elements with
# sum of the first that many natural
# numbers less than or equal to K
def Count(N, K):
     
    # If K equals to 0
    if (K == 0):
        return 0
 
    # Stores the result
    res = 0
 
    low = 1
    high = N
 
    # Iterate until low is less than
    # or equal to high
    while (low <= high):
        mid = (low + high) // 2
 
        # Stores the sum of first mid
        # natural numbers
        sum = (mid * mid + mid) // 2
         
        # If sum is less than or equal
        # to K
        if (sum <= K):
             
            # Update res and low
            res = max(res, mid)
            low = mid + 1
         
        # Otherwise,
        else:
             
            # Update
            high = mid - 1
             
    # Return res
    return res
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    N = 6
    K = 14
 
    # Function call
    print(Count(N, K))
     
# This code is contributed by shivanisinghss2110

C#

// C# program for above approach
using System;
 
class GFG{
 
// Function to count the elements with
// sum of the first that many natural
// numbers less than or equal to K
static int Count(int N, int K)
{
     
    // If K equals to 0
    if (K == 0)
        return 0;
 
    // Stores the result
    int res = 0;
 
    int low = 1, high = N;
 
    // Iterate until low is less than
    // or equal to high
    while (low <= high)
    {
        int mid = (low + high) / 2;
 
        // Stores the sum of first mid
        // natural numbers
 
        int sum = (mid * mid + mid) / 2;
         
        // If sum is less than or equal
        // to K
        if (sum <= K)
        {
             
            // Update res and low
            res = Math.Max(res, mid);
            low = mid + 1;
        }
       
        // Otherwise,
        else
        {
             
            // Update
            high = mid - 1;
        }
    }
 
    // Return res
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Input
    int N = 6, K = 14;
 
    // Function call
    Console.Write (Count(N, K));
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出
4

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