# Split a Numeric String into Fibonacci Sequence

Given a numeric string S representing a large number, the task is to form a Fibonacci Sequence of at least length 3 from the given string. If such a split is not possible, print -1.

Examples:

Input: S = “5712”
Output: 5 7 12
Explanation:
Since 5 + 7 = 12, the splits {5}, {7}, {12} forms a Fibonacci sequence.

Input: S = “11235813″
Output: 1 1 2 3 5 8 13

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To solve the problem, the idea is to use Backtracking to find a sequence that follows the conditions of the Fibonacci Sequence.
Follow the steps below to solve the problem:

1. Initialize a vector seq[] to store the Fibonacci sequence.
2. Initialize a variable pos which points to the current index of the string S, initially 0.
3. Iterate over the indices [pos, length – 1]:
• Add the number S[pos: i] to the Fibonacci sequence seq if the length of seq is less than 2 or the current number is equal to the sum of the last two numbers of seq. Recur for the index i + 1 and proceed.
• If the last added number S[pos: i] does not form a Fibonacci sequence and returns false after recursion, then remove it from the seq.
• Otherwise, end the loop and return true as the Fibonacci sequence is formed.
4. If pos exceeds the length of S, then:
• If the length of the sequence seq is greater than or equal to 3, then a Fibonacci sequence is found, hence return true.
• Otherwise, the Fibonacci sequence is not possible and hence returns false.
5. Finally, if the length of seq is greater than or equal to 3, then print the numbers in seq as the required Fibonacci sequence or, otherwise print -1.

Below is the illustration of the recursive structure where only one branch is extended to get the result: Below is the implementation of the above approach:

## C++

 `// C++ program of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `#define LL long long ` ` `  `// Function that returns true if ` `// Fibonacci sequence is found ` `bool` `splitIntoFibonacciHelper(``int` `pos, ` `                              ``string S, ` `                              ``vector<``int``>& seq) ` `{ ` `    ``// Base condition: ` `    ``// If pos is equal to length of S ` `    ``// and seq length is greater than 3 ` `    ``if` `(pos == S.length() ` `        ``and (seq.size() >= 3)) { ` ` `  `        ``// Return true ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Stores current number ` `    ``LL num = 0; ` ` `  `    ``for` `(``int` `i = pos; i < S.length(); i++) { ` ` `  `        ``// Add current digit to num ` `        ``num = num * 10 + (S[i] - ``'0'``); ` ` `  `        ``// Avoid integer overflow ` `        ``if` `(num > INT_MAX) ` `            ``break``; ` ` `  `        ``// Avoid leading zeros ` `        ``if` `(S[pos] == ``'0'` `and i > pos) ` `            ``break``; ` ` `  `        ``// If current number is greater ` `        ``// than last two number of seq ` `        ``if` `(seq.size() > 2 ` `            ``and (num > ((LL)seq.back() ` `                        ``+ (LL)seq[seq.size() ` `                                  ``- 2]))) ` `            ``break``; ` ` `  `        ``// If seq length is less ` `        ``// 2 or current number is ` `        ``// is equal to the last ` `        ``// two of the seq ` `        ``if` `(seq.size() < 2 ` `            ``or (num == ((LL)seq.back() ` `                        ``+ (LL)seq[seq.size() ` `                                  ``- 2]))) { ` ` `  `            ``// Add to the seq ` `            ``seq.push_back(num); ` ` `  `            ``// Recur for i+1 ` `            ``if` `(splitIntoFibonacciHelper(i + 1, ` `                                         ``S, seq)) ` `                ``return` `true``; ` ` `  `            ``// Remove last added number ` `            ``seq.pop_back(); ` `        ``} ` `    ``} ` ` `  `    ``// If no sequence is found ` `    ``return` `false``; ` `} ` ` `  `// Function that prints the Fibonacci ` `// sequence from the split of string S ` `void` `splitIntoFibonacci(string S) ` `{ ` `    ``// Initialize a vector to ` `    ``// store the sequence ` `    ``vector<``int``> seq; ` ` `  `    ``// Call helper function ` `    ``splitIntoFibonacciHelper(0, S, ` `                             ``seq); ` ` `  `    ``// If sequence length is ` `    ``// greater than 3 ` `    ``if` `(seq.size() >= 3) { ` ` `  `        ``// Print the sequence ` `        ``for` `(``int` `i : seq) ` `            ``cout << i << ``" "``; ` `    ``} ` ` `  `    ``// If no sequence is found ` `    ``else` `{ ` ` `  `        ``// Print -1 ` `        ``cout << -1; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given String ` `    ``string S = ``"11235813"``; ` ` `  `    ``// Function Call ` `    ``splitIntoFibonacci(S); ` `    ``return` `0; ` `} `

## Java

 `// Java program of the above approach  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function that returns true if ` `// Fibonacci sequence is found ` `static` `boolean` `splitIntoFibonacciHelper(``int` `pos, ` `                                        ``String S, ` `                              ``ArrayList seq) ` `{ ` `     `  `    ``// Base condition: ` `    ``// If pos is equal to length of S ` `    ``// and seq length is greater than 3 ` `    ``if` `(pos == S.length() && (seq.size() >= ``3``)) ` `    ``{ ` ` `  `        ``// Return true ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Stores current number ` `    ``long` `num = ``0``; ` ` `  `    ``for``(``int` `i = pos; i < S.length(); i++) ` `    ``{ ` `         `  `        ``// Add current digit to num ` `        ``num = num * ``10` `+ (S.charAt(i) - ``'0'``); ` ` `  `        ``// Avoid integer overflow ` `        ``if` `(num > Integer.MAX_VALUE) ` `            ``break``; ` ` `  `        ``// Avoid leading zeros ` `        ``if` `(S.charAt(pos) == ``'0'` `&& i > pos) ` `            ``break``; ` ` `  `        ``// If current number is greater ` `        ``// than last two number of seq ` `        ``if` `(seq.size() > ``2` `&&  ` `           ``(num > ((``long``)seq.get(seq.size() - ``1``) + ` `                   ``(``long``)seq.get(seq.size() - ``2``)))) ` `            ``break``; ` ` `  `        ``// If seq length is less ` `        ``// 2 or current number is ` `        ``// is equal to the last ` `        ``// two of the seq ` `        ``if` `(seq.size() < ``2` `|| ` `            ``(num == ((``long``)seq.get(seq.size() - ``1``) + ` `                     ``(``long``)seq.get(seq.size() - ``2``)))) ` `        ``{ ` `             `  `            ``// Add to the seq ` `            ``seq.add(num); ` ` `  `            ``// Recur for i+1 ` `            ``if` `(splitIntoFibonacciHelper(i + ``1``, ` `                                         ``S, seq)) ` `                ``return` `true``; ` ` `  `            ``// Remove last added number ` `            ``seq.remove(seq.size() - ``1``); ` `        ``} ` `    ``} ` `     `  `    ``// If no sequence is found ` `    ``return` `false``; ` `} ` ` `  `// Function that prints the Fibonacci ` `// sequence from the split of string S ` `static` `void` `splitIntoFibonacci(String S) ` `{ ` `     `  `    ``// Initialize a vector to ` `    ``// store the sequence ` `    ``ArrayList seq = ``new` `ArrayList<>(); ` ` `  `    ``// Call helper function ` `    ``splitIntoFibonacciHelper(``0``, S, seq); ` ` `  `    ``// If sequence length is ` `    ``// greater than 3 ` `    ``if` `(seq.size() >= ``3``)  ` `    ``{ ` `         `  `        ``// Print the sequence ` `        ``for` `(``int` `i = ``0``; i < seq.size(); i++) ` `            ``System.out.print(seq.get(i) + ``" "``); ` `    ``} ` ` `  `    ``// If no sequence is found ` `    ``else`  `    ``{ ` ` `  `        ``// Print -1 ` `        ``System.out.print(``"-1"``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `     `  `    ``// Given String ` `    ``String S = ``"11235813"``; ` `     `  `    ``// Function Call ` `    ``splitIntoFibonacci(S); ` `} ` `} ` ` `  `// This code is contributed by offbeat `

Output:

```1 1 2 3 5 8 13
```

Time Complexity: O(N2)
Space Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : offbeat

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.