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 = 90Input: 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
// 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; } |
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.
Recommended Posts:
- Sum of multiples of Array elements within a given range [L, R]
- Count of common multiples of two numbers in a range
- Count numbers from given range having odd digits at odd places and even digits at even places
- Sum of multiples of a number up to N
- Find the sum of all multiples of 2 and 5 below N
- Sum of all the multiples of 3 and 7 below N
- Sum of multiples of A and B less than N
- Minimum sum after subtracting multiples of k from the elements of the array
- Sum of the multiples of two numbers below N
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Multiples of 3 or 7
- N-th multiple in sorted list of multiples of two numbers
- Queries for counts of multiples in an array
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Sort elements of the array that occurs in between multiples of K
- Count common elements in two arrays containing multiples of N and M
- Count of Multiples of A ,B or C less than or equal to N
- Nth number in a set of multiples of A , B or C
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.

