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

📅  最后修改于: 2021-04-27 20:00:41             🧑  作者: 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


输出:
180

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