Given a number N. The task is to find the maximum possible value of SumOfDigits(A) + SumOfDigits(B) such that A + B = n (0<=A, B<=n).
Examples:
Input: N = 35
Output: 17
35 = 9 + 26
SumOfDigits(26) = 8, SumOfDigits(9) = 9
So, 17 is the answer.
Input: N = 7
Output: 7
Approach: The idea is to divide the number into two parts A and B such that A is in terms of 9 i.e. closest number to N and B = N-A. For example, N = 35, Smallest Number that is closest to 35 is 29.
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfDigitsSingle(int x)
{
int ans = 0;
while (x) {
ans += x % 10;
x /= 10;
}
return ans;
}
int closest(int x)
{
int ans = 0;
while (ans * 10 + 9 <= x)
ans = ans * 10 + 9;
return ans;
}
int sumOfDigitsTwoParts(int N)
{
int A = closest(N);
return sumOfDigitsSingle(A) + sumOfDigitsSingle(N - A);
}
int main()
{
int N = 35;
cout << sumOfDigitsTwoParts(N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int sumOfDigitsSingle(int x)
{
int ans = 0;
while (x != 0)
{
ans += x % 10;
x /= 10;
}
return ans;
}
static int closest(int x)
{
int ans = 0;
while (ans * 10 + 9 <= x)
ans = ans * 10 + 9;
return ans;
}
static int sumOfDigitsTwoParts(int N)
{
int A = closest(N);
return sumOfDigitsSingle(A) +
sumOfDigitsSingle(N - A);
}
public static void main(String args[])
{
int N = 35;
System.out.print(sumOfDigitsTwoParts(N));
}
}
|
Python3
def sumOfDigitsSingle(x) :
ans = 0
while x :
ans += x % 10
x //= 10
return ans
def closest(x) :
ans = 0
while (ans * 10 + 9 <= x) :
ans = ans * 10 + 9
return ans
def sumOfDigitsTwoParts(N) :
A = closest(N)
return sumOfDigitsSingle(A) + sumOfDigitsSingle(N - A)
if __name__ == "__main__" :
N = 35
print(sumOfDigitsTwoParts(N))
|
C#
using System;
class GFG
{
static int sumOfDigitsSingle(int x)
{
int ans = 0;
while (x != 0)
{
ans += x % 10;
x /= 10;
}
return ans;
}
static int closest(int x)
{
int ans = 0;
while (ans * 10 + 9 <= x)
ans = ans * 10 + 9;
return ans;
}
static int sumOfDigitsTwoParts(int N)
{
int A = closest(N);
return sumOfDigitsSingle(A) +
sumOfDigitsSingle(N - A);
}
public static void Main()
{
int N = 35;
Console.Write(sumOfDigitsTwoParts(N));
}
}
|
PHP
<?php
function sumOfDigitsSingle($x)
{
$ans = 0;
while ($x)
{
$ans += $x % 10;
$x /= 10;
}
return $ans;
}
function closest($x)
{
$ans = 0;
while ($ans * 10 + 9 <= $x)
$ans = $ans * 10 + 9;
return $ans;
}
function sumOfDigitsTwoParts($N)
{
$A = closest($N);
return sumOfDigitsSingle($A) +
sumOfDigitsSingle($N - $A);
}
$N = 35;
echo sumOfDigitsTwoParts($N);
?>
|
Javascript
<script>
function sumOfDigitsSingle(x)
{
let ans = 0;
while (x)
{
ans += x % 10;
x = Math.floor(x / 10);
}
return ans;
}
function closest(x)
{
let ans = 0;
while(ans * 10 + 9 <= x)
ans = ans * 10 + 9;
return ans;
}
function sumOfDigitsTwoParts(N)
{
let A = closest(N);
return sumOfDigitsSingle(A) +
sumOfDigitsSingle(N - A);
}
let N = 35;
document.write(sumOfDigitsTwoParts(N));
</script>
|
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.