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
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:
- Initialize a vector seq[] to store the Fibonacci sequence.
- Initialize a variable pos which points to the current index of the string S, initially 0.
- 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.
- 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.
- 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 <bits/stdc++.h> 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; } |
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.
Recommended Posts:
- Split numeric, alphabetic and special symbols from a String
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Print Fibonacci sequence using 2 variables
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Remove characters from a numeric string such that string becomes divisible by 8
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Split the string into minimum parts such that each part is in the another string
- Splitting a Numeric String
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Python Regex to extract maximum numeric value from a string
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Split the given string into Odds: Digit DP
- Split the string into substrings using delimiter
- Split the given string into Primes : Digit DP
- How to split a string in C/C++, Python and Java?
- Check if given string can be split into four distinct strings
- Find all possible ways to Split the given string into Primes
- Split the binary string into substrings with equal number of 0s and 1s
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.

