Find B and C with Specific Digit Sums for Integer A
Last Updated :
06 Apr, 2023
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++
#include <bits/stdc++.using namespace std;
#define int long long
void Solve( int A)
{
int B = 2 * A;
string C;
int x = A / 4;
int rem = A % 4;
if (rem != 0) {
C += to_string(rem);
}
for ( int i = 0; i < x; i++) {
C += '4' ;
}
cout << "B = " << B << ", "
<< "C = " << C;
}
int32_t main()
{
int A = 3;
Solve(A);
}
|
Java
import java.util.*;
public class Main {
public static void solve( int A) {
int B = 2 * A;
StringBuilder C = new StringBuilder();
int x = A / 4 ;
int rem = A % 4 ;
if (rem != 0 ) {
C.append(rem);
}
for ( int i = 0 ; i < x; i++) {
C.append( '4' );
}
System.out.println( "B = " + B + ", " + "C = " + C.toString());
}
public static void main(String[] args) {
int A = 3 ;
solve(A);
}
}
|
Python3
def Solve(A):
B = 2 * A
C = ""
x = A / / 4
rem = A % 4
if rem ! = 0 :
C + = str (rem)
for i in range (x):
C + = '4'
print (f "B = {B}, C = {C}" )
if __name__ = = "__main__" :
A = 3
Solve(A)
|
C#
using System;
class GFG {
static void Solve( int A) {
int B = 2 * A;
string C = "" ;
int x = A / 4;
int rem = A % 4;
if (rem != 0) {
C += rem.ToString();
}
for ( int i = 0; i < x; i++) {
C += '4' ;
}
Console.Write( "B = " + B + ", "
+ "C = " + C);
}
static void Main() {
int A = 3;
Solve(A);
}
}
|
Javascript
function Solve(A) {
let B = 2 * A;
let C = "" ;
let x = Math.floor(A / 4);
let rem = A % 4;
if (rem !== 0) {
C += rem.toString();
}
for (let i = 0; i < x; i++) {
C += '4' ;
}
console.log( "B = " + B + ", " + "C = " + C);
}
let A = 3;
Solve(A);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...