📜  给定范围内 K 的最大可能和 5 的倍数

📅  最后修改于: 2021-10-26 03:04:59             🧑  作者: Mango

给定三个整数L、RK ,任务是从范围[L, R] 中找到K偶数5的最大和。

例子:

朴素的方法:最简单的方法是迭代从RL 的所有偶数元素,并且对于每个元素,检查它是否可以被 5 整除。如果发现为真,则将该元素添加到总和并递减K 。当K达到 0 时,中断循环并打印获得的总和。

时间复杂度: O(N),其中 N=RL
辅助空间: O(1)

高效方法:上述方法可以基于以下观察进行优化:

请按照以下步骤解决问题:

  • 初始化N = (R / 10 – L / 10) + 1以存储范围[L, R]中 5 的偶数倍数的计数。
  • 如果K > N ,打印-1表示在[L, R]范围内有小于K 的偶数5
  • 除此以外:
    • 初始化M = R – K
    • 存储总和 = 10 * (N * (N + 1) / 2 – M * (M + 1) / 2)
    • 打印总和

下面是上述方法的实现:

C++14
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum of K
// even multiples of 5 in the range [L, R]
void maxksum(int L, int R, int K)
{
    // Store the total number of even
    // multiples of 5 in the range [L, R]
    int N = (R / 10 - L / 10) + 1;
 
    // Check if K > N
    if (K > N) {
 
        // If true, print -1 and return
        cout << -1;
        return;
    }
 
    // Otherwise, divide R by 10
    R = R / 10;
 
    // Store the sum using the formula
    int X = R - K;
    int sum = 10 * ((R * (R + 1)) / 2
                    - (X * (X + 1)) / 2);
 
    // Print the sum
    cout << sum;
}
 
// Driver Code
int main()
{
 
    // Given L, R and K
    int L = 16, R = 60, K = 4;
 
    // Function Call
    maxksum(L, R, K);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Function to find the maximum sum of K
// even multiples of 5 in the range [L, R]
static void maxksum(int L, int R, int K)
{
     
    // Store the total number of even
    // multiples of 5 in the range [L, R]
    int N = (R / 10 - L / 10) + 1;
 
    // Check if K > N
    if (K > N)
    {
         
        // If true, print -1 and return
        System.out.print("-1");
        return;
    }
     
    // Otherwise, divide R by 10
    R = R / 10;
 
    // Store the sum using the formula
    int X = R - K;
    int sum = 10 * ((R * (R + 1)) / 2 -
                    (X * (X + 1)) / 2);
 
    // Print the sum
    System.out.print( sum);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given L, R and K
    int L = 16, R = 60, K = 4;
     
    // Function Call
    maxksum(L, R, K);
}
}
 
// This code is contributed by Stream_Cipher


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum sum of K
# even multiples of 5 in the range [L, R]
def maxksum(L, R, K) :
 
    # Store the total number of even
    # multiples of 5 in the range [L, R]
    N = (R // 10 - L // 10) + 1;
 
    # Check if K > N
    if (K > N) :
 
        # If true, print -1 and return
        print(-1);
        return;
 
    # Otherwise, divide R by 10
    R = R // 10;
 
    # Store the sum using the formula
    X = R - K;
    sum = 10 * ((R * (R + 1)) // 2 - (X * (X + 1)) // 2);
 
    # Print the sum
    print(sum);
 
# Driver Code
if __name__ == "__main__" :
 
    # Given L, R and K
    L = 16; R = 60; K = 4;
 
    # Function Call
    maxksum(L, R, K);
 
    # This code is contributed by AnkThon


C#
// C# program to implement
// the above approach 
using System;
class GFG
{
  
// Function to find the maximum sum of K
// even multiples of 5 in the range [L, R]
static void maxksum(int L, int R, int K)
{
      
    // Store the total number of even
    // multiples of 5 in the range [L, R]
    int N = (R / 10 - L / 10) + 1;
  
    // Check if K > N
    if (K > N)
    {
          
        // If true, print -1 and return
        Console.Write("-1");
        return;
    }
      
    // Otherwise, divide R by 10
    R = R / 10;
  
    // Store the sum using the formula
    int X = R - K;
    int sum = 10 * ((R * (R + 1)) / 2 -
                    (X * (X + 1)) / 2);
  
    // Print the sum
    Console.Write( sum);
}
  
// Driver code
public static void Main()
{
   
    // Given L, R and K
    int L = 16, R = 60, K = 4;
      
    // Function Call
    maxksum(L, R, K);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
180

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