Find sub | Function to print the valid indices in the array ; Function to find sub - arrays from two different arrays with equal sum ; Map to store the indices in A and B which produce the given difference ; Find the smallest j such that b [ j ] >= a [ i ] ; Difference encountered for the second time ; b [ j ] - a [ i ] = b [ idx . second ] - a [ idx . first ] b [ j ] - b [ idx . second ] = a [ i ] = a [ idx . first ] So sub - arrays are a [ idx . first + 1. . . i ] and b [ idx . second + 1. . . j ] ; Store the indices for difference in the map ; Utility function to calculate the cumulative sum of the array ; Driver code ; Function to update the arrays with their cumulative sum ; Swap is true as a and b are swapped during function call
def printAns ( x , y , num ) : NEW_LINE INDENT print ( " Indices ▁ in ▁ array " , num , " : " , end = " ▁ " ) NEW_LINE for i in range ( x , y ) : NEW_LINE INDENT print ( i , end = " , ▁ " ) NEW_LINE DEDENT print ( y ) NEW_LINE DEDENT def findSubarray ( N , a , b , swap ) : NEW_LINE INDENT index = { } NEW_LINE difference , j = 0 , 0 NEW_LINE index [ 0 ] = ( - 1 , - 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT while b [ j ] < a [ i ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT difference = b [ j ] - a [ i ] NEW_LINE if difference in index : NEW_LINE INDENT if swap : NEW_LINE INDENT idx = index [ b [ j ] - a [ i ] ] NEW_LINE printAns ( idx [ 1 ] + 1 , j , 1 ) NEW_LINE printAns ( idx [ 0 ] + 1 , i , 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT idx = index [ b [ j ] - a [ i ] ] NEW_LINE printAns ( idx [ 0 ] + 1 , i , 1 ) NEW_LINE printAns ( idx [ 1 ] + 1 , j , 2 ) NEW_LINE DEDENT return NEW_LINE DEDENT index [ difference ] = ( i , j ) NEW_LINE DEDENT print ( " - 1" ) NEW_LINE DEDENT def cumulativeSum ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE b = [ 6 , 2 , 1 , 5 , 4 ] NEW_LINE N = len ( a ) NEW_LINE cumulativeSum ( a , N ) NEW_LINE cumulativeSum ( b , N ) NEW_LINE if b [ N - 1 ] > a [ N - 1 ] : NEW_LINE INDENT findSubarray ( N , a , b , False ) NEW_LINE DEDENT else : NEW_LINE INDENT findSubarray ( N , b , a , True ) NEW_LINE DEDENT DEDENT