-
Notifications
You must be signed in to change notification settings - Fork 0
/
Additive sequence.cpp
68 lines (62 loc) · 1.4 KB
/
Additive sequence.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Recursion
class Solution {
public:
int length(int n)
{
int len=0;
while(n)
{
len++;
n/=10;
}
return len;
}
bool help(string s,int i,int first,int second)
{
//base case
if(i>=s.length())
return 0;
//recursive calls
//and small calculation
if(first!=-1 and second!=-1)
{
int add=first+second;
int len=length(add);
int num=0;
int j=i;
while(j<s.length() and j<(i+len))
{
num*=10;
num+=(s[j]-'0');
j++;
}
if(num!=add)
return 0;
if(j==s.length())
return 1;
return help(s,j,second,num);
}
int num=0;
for(int j=i;j<s.length();j++)
{
num*=10;
num+=(s[j]-'0');
if(first!=-1)
{
bool call=help(s,j+1,first,num);
if(call)
return 1;
}
else
{
bool call=help(s,j+1,num,second);
if(call)
return 1;
}
}
return 0;
}
bool isAdditiveSequence(string n) {
return help(n,0,-1,-1);
}
};