Skip to content

Commit 70de983

Browse files
committed
Added maximum sum of digits
1 parent cea03fd commit 70de983

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Maximum sum of digits/code.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
#define MAX 100005
3+
#define pb push_back
4+
#define mp make_pair
5+
#define M 1000000007
6+
#define fi first
7+
#define se second
8+
#define inp(i, n, a) for(int i=0;i<n;i++) cin>>a[i];
9+
#define int long long
10+
using namespace std;
11+
12+
int sod(int n)
13+
{
14+
int res = 0;
15+
while(n)
16+
{
17+
res = res + n%10;
18+
n /= 10;
19+
}
20+
return res;
21+
}
22+
23+
signed main()
24+
{
25+
// ios_base::sync_with_stdio(false);
26+
// cin.tie(NULL);
27+
28+
string n;
29+
cin >> n;
30+
string num1 = "", num2 = "";
31+
for(int i=0; i<n.length(); i++)
32+
{
33+
if(i==0)
34+
num1 += (n[i]-1);
35+
else
36+
num1 += "9";
37+
}
38+
int n1 = 0, ori = 0;
39+
for(int i=0; i<num1.length(); i++)
40+
{
41+
n1 = n1 * 10 + (num1[i]-'0');
42+
ori = ori * 10 + (n[i]-'0');
43+
}
44+
cout << sod(n1) + sod(ori-n1);
45+
return 0;
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
You are given a positive integer n.
2+
3+
Let S(x) be sum of digits in base 10 representation of x, for example, S(123)=1 + 2 + 3 = 6, S(0) = 0.
4+
5+
Your task is to find two integers a,b, such that 0 ≤ a,b ≤n, a + b = n and S(a) + S(b) is the largest possible among all such pairs.
6+
7+
Input
8+
The only line of input contains an integer n (1 ≤ n ≤ 1012).
9+
10+
Output
11+
Print largest S(a) + S(b) among all pairs of integers a, b , such that 0 ≤ a, b ≤ n and a + b = n.

0 commit comments

Comments
 (0)