We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here .
if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } function equiSum ( arr ) { let ans = equiSumUtil ( arr ) ; if ( ans ) { document . write ( " " ) ; for ( let i = 0 ; i <= pos1 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = pos1 + 1 ; i < pos2 ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; document . write ( " " ) ; for ( let i = pos2 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } else { document . writeLine ( " " + " " ) ; } }