Maximum possible sum of K even multiples of 5 in a given range

Given three integers L, R and K, the task is to find the maximum sum of K even multiples of 5 from the range [L, R].

Examples:

Input: L = 7, R = 48, K = 3
Output: 90
Explanation: Maximum sum of 3 even multiples of 5 in the range [7, 48] = 40 + 30 + 20 = 90

Input: L = 16, R= 60, K = 4
Output: 180
Explanation: Maximum sum of 4 even multiples of 5 in the range [16, 60] = 60 + 50 + 40 + 30 = 180

Naive Approach: The simplest approach is to iterate over all even elements from R to L, and for every element, check if it is divisible by 5 or not. If found to be true, add that element to the sum and decrement K. When K reaches 0, break the loop and print the obtained sum.
Time Complexity: O(N), where N=R-L
Auxiliary Space: O(1)



Efficient Approach: The above approach can be optimized based on the following observation:

Count of even multiples of 5 in the range [1, X] = X / 10
Count of even multiples of 5 in range [L, R] = (R / 10 – L / 10) + 1 = N(say)
Maximum sum of K even multiples of 5 in range [L, R]
= Sum of even multiples of 5 in range [1, R] – Sum of even multiples of 5 in range [1, R – K]
= 10 * (N * (N + 1) / 2 – M * (M + 1) / 2), where M = R – K

Follow the steps below to solve the problem:

  • Initialize N = (R / 10 – L / 10) + 1 to store the count of even multiples of 5 in the range [L, R].
  • If K > N, print -1 indicating that there are less than K even multiples of 5 in the range [L, R].
  • Otherwise:
    • Initialize M = R – K.
    • Store sum = 10 * (N * (N + 1) / 2 – M * (M + 1) / 2).
    • Print sum.

Below is the implementation of the above approach:

C++14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
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;
}

chevron_right


Output:

180

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Coding Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.