-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.cpp
More file actions
54 lines (40 loc) · 1.09 KB
/
Copy path18.cpp
File metadata and controls
54 lines (40 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
// SUM OF ARRAY ********************************
// sum(n, arr) -> sum of elements up to n index
// in recursive function define the function in its own terms
// sum(n, a) = a[n] + sum(n-1, a)
// int sum(int n, int a[]){
// if(n < 0) return 0;
// return sum(n-1, a) + a[n];
// }
// // O(N * 1) = O(N)
// int main() {
// int n;
// cin>>n;
// int a[n];
// for (int i = 0; i < n; i++)
// {
// cin>>a[i];
// }
// cout<<sum(3, a)<<"\n";
// return 0;
// }
// DIGIT SUM **************************
// 1234 = 4 + digit_sum(123)
// digit_sum(n) -> digit_sum(n/10) + last_digit
int digit_sum(int n){
if(n==0) return 0;
return digit_sum(n/10) + (n % 10);
}
int main(){
int n;
cin>>n;
cout<<digit_sum(n)<<"\n";
return 0;
}
// 1. Number of function calls -> no. of digits in n
// 2. complexity of each function call -> O(1)
// log(N) -> no. of digits in number n
// how many times we can divide N with 10 is what log(N) is ********************************
// thus O(logN) is the time complexity