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 a pair ; Two pointers l and r ; Stores prefix sum array ; Traverse the array ; Two pointer approach ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Print split indices if sum is equal ; Move left pointer if lsum < rsum ; Move right pointer if rsum > lsum ; Move both pointers if lsum = rsum but they are not equal to msum ; If no possible pair exists , print - 1 ; Driver Code ; Given array ; Size of the array
def findSplit ( arr , N ) : NEW_LINE INDENT l = 1 ; r = N - 2 ; NEW_LINE sum = [ 0 ] * N ; NEW_LINE sum [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT sum [ i ] = sum [ i - 1 ] + arr [ i ] ; NEW_LINE DEDENT while ( l < r ) : NEW_LINE INDENT lsum = sum [ l - 1 ] ; NEW_LINE msum = sum [ r - 1 ] - sum [ l ] ; NEW_LINE rsum = sum [ N - 1 ] - sum [ r ] ; NEW_LINE if ( lsum == msum and msum == rsum ) : NEW_LINE INDENT print ( l , r ) ; NEW_LINE return ; NEW_LINE DEDENT if ( lsum < rsum ) : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT elif ( lsum > rsum ) : NEW_LINE INDENT r -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 ; NEW_LINE r -= 1 ; NEW_LINE DEDENT DEDENT print ( - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findSplit ( arr , N ) ; NEW_LINE DEDENT