Maximum possible sum of K even multiples of 5 in a given range
Last Updated :
05 May, 2021
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
#include <bits/stdc++.h>
using namespace std;
void maxksum( int L, int R, int K)
{
int N = (R / 10 - L / 10) + 1;
if (K > N) {
cout << -1;
return ;
}
R = R / 10;
int X = R - K;
int sum = 10 * ((R * (R + 1)) / 2
- (X * (X + 1)) / 2);
cout << sum;
}
int main()
{
int L = 16, R = 60, K = 4;
maxksum(L, R, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxksum( int L, int R, int K)
{
int N = (R / 10 - L / 10 ) + 1 ;
if (K > N)
{
System.out.print( "-1" );
return ;
}
R = R / 10 ;
int X = R - K;
int sum = 10 * ((R * (R + 1 )) / 2 -
(X * (X + 1 )) / 2 );
System.out.print( sum);
}
public static void main(String[] args)
{
int L = 16 , R = 60 , K = 4 ;
maxksum(L, R, K);
}
}
|
Python3
def maxksum(L, R, K) :
N = (R / / 10 - L / / 10 ) + 1 ;
if (K > N) :
print ( - 1 );
return ;
R = R / / 10 ;
X = R - K;
sum = 10 * ((R * (R + 1 )) / / 2 - (X * (X + 1 )) / / 2 );
print ( sum );
if __name__ = = "__main__" :
L = 16 ; R = 60 ; K = 4 ;
maxksum(L, R, K);
|
C#
using System;
class GFG
{
static void maxksum( int L, int R, int K)
{
int N = (R / 10 - L / 10) + 1;
if (K > N)
{
Console.Write( "-1" );
return ;
}
R = R / 10;
int X = R - K;
int sum = 10 * ((R * (R + 1)) / 2 -
(X * (X + 1)) / 2);
Console.Write( sum);
}
public static void Main()
{
int L = 16, R = 60, K = 4;
maxksum(L, R, K);
}
}
|
Javascript
<script>
function maxksum(L, R, K)
{
let N = (R / 10 - L / 10) + 1;
if (K > N)
{
document.write( "-1" );
return ;
}
R = R / 10;
let X = R - K;
let sum = 10 * ((R * (R + 1)) / 2 -
(X * (X + 1)) / 2);
document.write(sum);
}
let L = 16, R = 60, K = 4;
maxksum(L, R, K);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...