Split a Numeric String into Fibonacci Sequence | 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 ; Given String ; Function call
function splitIntoFibonacciHelper ( pos , S , seq ) { if ( pos == S . length && ( seq . length >= 3 ) ) { return true ; } let num = 0 ; for ( let i = pos ; i < S . length ; i ++ ) { num = num * 10 + ( S [ i ] - ' ' ) ; if ( num > Number . MAX_VALUE ) break ; if ( S [ pos ] == ' ' && i > pos ) break ; if ( seq . length > 2 && ( num > ( seq [ seq . length - 1 ] + seq [ seq . length - 2 ] ) ) ) break ; if ( seq . length < 2 || ( num == ( seq [ seq . length - 1 ] + seq [ seq . length - 2 ] ) ) ) { seq . push ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . pop ( ) ; } } return false ; } function splitIntoFibonacci ( S ) { let seq = [ ] ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . length >= 3 ) { for ( let i = 0 ; i < seq . length ; i ++ ) document . write ( seq [ i ] + " " ) ; } else { document . write ( " " ) ; } } let S = " " ; splitIntoFibonacci ( S ) ;