Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing two elements ; Stores sum of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no pair exists , print - 1 ; Given array ; Size of the array
function findSplit ( arr , N ) { for ( let l = 1 ; l <= N - 4 ; l ++ ) { for ( let r = l + 2 ; r <= N - 2 ; r ++ ) { let lsum = 0 , rsum = 0 , msum = 0 ; for ( let i = 0 ; i <= l - 1 ; i ++ ) { lsum += arr [ i ] ; } for ( let i = l + 1 ; i <= r - 1 ; i ++ ) { msum += arr [ i ] ; } for ( let i = r + 1 ; i < N ; i ++ ) { rsum += arr [ i ] ; } if ( lsum == rsum && rsum == msum ) { document . write ( l + " " + r + " " ) ; return ; } } } document . write ( - 1 ) ; } let arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] ; let N = arr . length ; findSplit ( arr , N ) ;