Split Array into Fibonacci Sequence - Problem
You are given a string of digits num, such as "123456579". We can split it into a Fibonacci-like sequence [123, 456, 579].
Formally, a Fibonacci-like sequence is a list f of non-negative integers such that:
0 <= f[i] < 2³¹(each integer fits in a 32-bit signed integer type)f.length >= 3f[i] + f[i + 1] == f[i + 2]for all0 <= i < f.length - 2
Note: When splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0 itself.
Return any Fibonacci-like sequence split from num, or return an empty array if it cannot be done.
Input & Output
Example 1 — Valid Fibonacci Sequence
$
Input:
num = "123456579"
›
Output:
[123,456,579]
💡 Note:
We can split the string as "123" + "456" + "579". Since 123 + 456 = 579, this forms a valid Fibonacci-like sequence.
Example 2 — Simple Three Numbers
$
Input:
num = "11235813"
›
Output:
[1,1,2,3,5,8,13]
💡 Note:
Split as 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8, 5 + 8 = 13. Classic Fibonacci sequence.
Example 3 — No Valid Split
$
Input:
num = "199100199"
›
Output:
[]
💡 Note:
No way to split this string into a valid Fibonacci-like sequence of 3 or more numbers.
Constraints
- 1 ≤ num.length ≤ 200
- num contains only digits
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string of digits: "123456579"
2
Try Splits
Attempt different ways to split into numbers
3
Validate Fibonacci
Check if f[i] + f[i+1] = f[i+2] for all valid i
4
Result
Return valid sequence or empty array
Key Takeaway
🎯 Key Insight: Once the first two numbers are chosen, the entire Fibonacci sequence is uniquely determined
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code