Check if a large number can be divided into two or more segments of equal sum | Function to check if a number can be divided into segments ; length of string ; array to store prefix sum ; first index ; calculate the prefix ; iterate for all number from second number ; sum from 0 th index to i - 1 th index ; counter turns true when sum is obtained from a segment ; iterate till the last number ; sum of segments ; if segment sum is equal to first segment ; when greater than not possible ; if at the end all values are traversed and all segments have sum equal to first segment then it is possible ; Driver Code
function check ( s ) { let n = s . length ; var Presum = [ ] ; Presum . push ( parseInt ( s [ 0 ] ) ) ; let i ; for ( i = 1 ; i < n ; i ++ ) { Presum . push ( Presum [ i - 1 ] + parseInt ( s [ i ] ) ) ; } for ( i = 1 ; i <= n - 1 ; i ++ ) { let sum = Presum [ i - 1 ] ; let presum = 0 ; let it = i ; let flag = 0 ; while ( it < n ) { presum += parseInt ( s [ it ] ) ; if ( presum == sum ) { presum = 0 ; flag = 1 ; } else if ( presum > sum ) { break ; } it ++ ; } if ( presum == 0 && it == n && flag == 1 ) { return true ; } } return false ; } var s = " " ; if ( check ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;