Split a Numeric String into Fibonacci Sequence | C # program of the above approach ; Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Driver Code ; Given String ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static bool splitIntoFibonacciHelper ( int pos , string S , ArrayList seq ) { if ( pos == S . Length && ( seq . Count >= 3 ) ) { return true ; } long num = 0 ; for ( int i = pos ; i < S . Length ; i ++ ) { num = num * 10 + ( S [ i ] - '0' ) ; if ( num > Int64 . MaxValue ) break ; if ( S [ pos ] == '0' && i > pos ) break ; if ( seq . Count > 2 && ( num > ( ( long ) seq [ seq . Count - 1 ] + ( long ) seq [ seq . Count - 2 ] ) ) ) break ; if ( seq . Count < 2 || ( num == ( ( long ) seq [ seq . Count - 1 ] + ( long ) seq [ seq . Count - 2 ] ) ) ) { seq . Add ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . Remove ( seq . Count - 1 ) ; } } return false ; } static void splitIntoFibonacci ( string S ) { ArrayList seq = new ArrayList ( ) ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . Count >= 3 ) { for ( int i = 0 ; i < seq . Count ; i ++ ) Console . Write ( seq [ i ] + " ▁ " ) ; } else { Console . Write ( " - 1" ) ; } } public static void Main ( string [ ] args ) { string S = "11235813" ; splitIntoFibonacci ( S ) ; } }