Given a positive integer A, the task is to find two positive integers, B and C. B is the maximum possible value and C is the minimum possible value such that the sum of digits of C is A and the sum of digits of twice of C is equal to B.
Examples:
Input: A = 3
Output: B = 6, C = 3
Explanation: It can be seen the sum of digits is equal to 3(=A) and twice that is 6(=B). Note that B = 6 and C = 12 are also possible to answer, but we need the maximum B and minimum C satisfying the given condition.Input: A = 100
Output: B = 200, C = 4444444444444444444444444
Approach: This can be solved with the following idea:
Observations:
Suppose there exists an integer C such that sum of digits of C is equal to A.
- Sum of digits of 2 * C ≤ 2 * A (As sum of digits of (a+b) = sum of digits of a + sum of digits of b – 9*(number of carries while calculating a+b by column method)).
- Sum of digits of 2 * C = 2 * A, only when all the digits of C are almost 4.
Therefore, based on observation(2), B = 2*A.
Now, we know that all digits of C are almost 4. To minimize the value of C, we will make the value of all of its digits except the most significant one equal to 4 (If possible, make a significant one also equal to 4).
The following steps can be used to solve the problem:
- Set the value of B = 2 * A.
- Initialize a string C.
- Find the remainder when A is divided by 4. Append that to C.
- Suppose x is the quotient when A is divided by 4. Append x 4s to the string C.
- Return the integer B and the string C.
Following is the code based on the above approach :
// C++ code for the above approach #include <bits/stdc++.using namespace std; #define int long long // Function to Find B and C such that // sum of digits of C is A and that // of 2C is equal to B. void Solve( int A)
{ // B would be simply twice of A
int B = 2 * A;
// Declaring C as a string as
// it can be very large
string C;
// x is the quotient when
// A is divided by 4
int x = A / 4;
// rem stores the value of remainder
// when A is divided by 4
int rem = A % 4;
// If remainder is not zero,
// append that to C
if (rem != 0) {
C += to_string(rem);
}
// Append x times 4s to C
for ( int i = 0; i < x; i++) {
C += '4' ;
}
// Printing the values of B and C
cout << "B = " << B << ", "
<< "C = " << C;
} // Driver code int32_t main() { int A = 3;
// Function call
Solve(A);
} |
import java.util.*;
public class Main {
// Function to Find B and C such that
// sum of digits of C is A and that
// of 2C is equal to B.
public static void solve( int A) {
// B would be simply twice of A
int B = 2 * A;
// Declaring C as a string as
// it can be very large
StringBuilder C = new StringBuilder();
// x is the quotient when
// A is divided by 4
int x = A / 4 ;
// rem stores the value of remainder
// when A is divided by 4
int rem = A % 4 ;
// If remainder is not zero,
// append that to C
if (rem != 0 ) {
C.append(rem);
}
// Append x times 4s to C
for ( int i = 0 ; i < x; i++) {
C.append( '4' );
}
// Printing the values of B and C
System.out.println( "B = " + B + ", " + "C = " + C.toString());
}
// Driver code
public static void main(String[] args) {
int A = 3 ;
// Function call
solve(A);
}
} |
# python code for the above approach # Function to Find B and C such that # sum of digits of C is A and that # of 2C is equal to B. def Solve(A):
# B would be simply twice of A
B = 2 * A
# Declaring C as a string as
# it can be very large
C = ""
# x is the quotient when
# A is divided by 4
x = A / / 4
# rem stores the value of remainder
# when A is divided by 4
rem = A % 4
# If remainder is not zero,
# append that to C
if rem ! = 0 :
C + = str (rem)
# Append x times 4s to C
for i in range (x):
C + = '4'
# Printing the values of B and C
print (f "B = {B}, C = {C}" )
# Driver code if __name__ = = "__main__" :
A = 3
# Function call
Solve(A)
|
// C# code for the approach using System;
class GFG {
// Function to Find B and C such that
// sum of digits of C is A and that
// of 2C is equal to B.
static void Solve( int A) {
// B would be simply twice of A
int B = 2 * A;
// Declaring C as a string as
// it can be very large
string C = "" ;
// x is the quotient when
// A is divided by 4
int x = A / 4;
// rem stores the value of remainder
// when A is divided by 4
int rem = A % 4;
// If remainder is not zero,
// append that to C
if (rem != 0) {
C += rem.ToString();
}
// Append x times 4s to C
for ( int i = 0; i < x; i++) {
C += '4' ;
}
// Printing the values of B and C
Console.Write( "B = " + B + ", "
+ "C = " + C);
}
// Driver code
static void Main() {
int A = 3;
// Function call
Solve(A);
}
} |
// Javascript code for the above approach // Function to Find B and C such that // sum of digits of C is A and that // of 2C is equal to B. function Solve(A) {
// B would be simply twice of A
let B = 2 * A;
// Declaring C as a string as
// it can be very large
let C = "" ;
// x is the quotient when
// A is divided by 4
let x = Math.floor(A / 4);
// rem stores the value of remainder
// when A is divided by 4
let rem = A % 4;
// If remainder is not zero,
// append that to C
if (rem !== 0) {
C += rem.toString();
}
// Append x times 4s to C
for (let i = 0; i < x; i++) {
C += '4' ;
}
// Printing the values of B and C
console.log( "B = " + B + ", " + "C = " + C);
} // Driver code let A = 3; // Function call Solve(A); |
B = 6, C = 3
Time Complexity: O(N)
Auxiliary Space: O(1)