Given three positive integers N, K, and M, the task is to find the number to be added to N to obtain the sum of first M multiples of K.
Examples:
Input: N = 17, K = 3, M = 4
Output: 13
Explanation:
Sum of first 4 multiples of 3 = (3 + 6 + 9 + 12) = 30.
Therefore, the value to be added to 17 is (30 – 17) = 13.
Therefore, the required output is 13.Input: N = 5, K = 2, M = 1
Output: -3
Explanation:
Sum of first 1 multiple of 2 is 2.
The value to be added to 5 to get 2 is (2 – 5) = -3
Approach: Follow the steps below to solve the problem:
- Calculate the sum of first M multiples of K, which will be equal to K * (1 + 2 + 3 + … M) = K * M * (M + 1) / 2.
- Initialize a variable, say res, to store the number required to be added to N to obtain sum.
- Therefore, res will be equal to sum – N. Print the value of res.
Below is the implementation of the above approach:
// C++ program for the above approach #include <iostream> using namespace std;
// Function to print the value // to be added to N to obtain // sum of first M multiples of K static int printNumber( int N, int K, int M)
{ // Store the sum of the
// first M multiples of K
int sum = K * (M * (M + 1) / 2);
// Store the value to be
// added to obtain N
return sum - N;
} // Driver Code int main()
{ // Input
int N = 17;
int K = 3;
int M = 4;
cout << printNumber(N, K, M);
return 0;
} // This code is contributed by shubhamsingh10 |
// Java code of above approach import java.util.*;
class GFG
{ // Function to print the value
// to be added to N to obtain
// sum of first M multiples of K
static int printNumber( int N, int K, int M)
{
// Store the sum of the
// first M multiples of K
int sum = K * (M * (M + 1 ) / 2 );
// Store the value to be
// added to obtain N
return sum - N;
}
// Driver code
public static void main(String[] args)
{
// Input
int N = 17 ;
int K = 3 ;
int M = 4 ;
System.out.print(printNumber(N, K, M));
}
} // This code is contributed by offbeat |
# Python3 program for the above approach # Function to print the value # to be added to N to obtain # sum of first M multiples of K def printNumber(N, K, M):
# Store the sum of the
# first M multiples of K
sum = K * (M * (M + 1 ) / 2 )
# Store the value to be
# added to obtain N
return sum - N
# Driver Code # Input N = 17
K = 3
M = 4
print ( int (printNumber(N, K, M)))
|
// C# program for the above approach using System;
class GFG
{ // Function to print the value
// to be added to N to obtain
// sum of first M multiples of K
static int printNumber( int N, int K, int M)
{
// Store the sum of the
// first M multiples of K
int sum = K * (M * (M + 1) / 2);
// Store the value to be
// added to obtain N
return sum - N;
}
// Driver code
public static void Main(String[] args)
{
// Input
int N = 17;
int K = 3;
int M = 4;
Console.Write(printNumber(N, K, M));
}
} // This code is contributed by shubhamsingh10 |
<script> // JavaScript program for the above approach // Function to print the value // to be added to N to obtain // sum of first M multiples of K function printNumber(N, K, M)
{ // Store the sum of the
// first M multiples of K
var sum = K * ((M * (M + 1)) / 2);
// Store the value to be
// added to obtain N
return sum - N;
} // Driver Code // Input var N = 17;
var K = 3;
var M = 4;
document.write(printNumber(N, K, M)); // This code is contributed by rdtank </script> |
13
Time Complexity: O(1)
Auxiliary Space: O(1)