Two equal sum segment range queries | Function to find the required prefix sum ; Function to hash all the values of prefix sum array in an unordered map ; Function to check if a range can be divided into two equal parts ; To store the value of sum of entire range ; If value of sum is odd ; To store p_arr [ l - 1 ] ; If the value exists in the map ; Driver code ; prefix - sum array ; Map to store the values of prefix - sum ; Perform queries
function prefixSum ( p_arr , arr , n ) { p_arr [ 0 ] = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] ; } function hashPrefixSum ( p_arr , n , q ) { for ( var i = 0 ; i < n ; i ++ ) q . add ( p_arr [ i ] ) ; } function canDivide ( p_arr , n , q , l , r ) { var sum ; if ( l == 0 ) sum = p_arr [ r ] ; else sum = p_arr [ r ] - p_arr [ l - 1 ] ; if ( sum % 2 == 1 ) { document . write ( " " ) ; return ; } var beg = 0 ; if ( l != 0 ) beg = p_arr [ l - 1 ] ; if ( q . has ( ( beg + sum / 2 ) ) ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , 1 , 2 , 3 ] ; var n = arr . length ; var p_arr = Array ( n ) ; prefixSum ( p_arr , arr , n ) ; var q = new Set ( ) ; hashPrefixSum ( p_arr , n , q ) ; canDivide ( p_arr , n , q , 0 , 1 ) ; canDivide ( p_arr , n , q , 1 , 3 ) ; canDivide ( p_arr , n , q , 1 , 2 ) ;