Smallest pair of indices with product of subarray co | Function to calculate GCD of two integers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then pr - 1 ; Driver Code ; Function Call
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def findPair ( A , N ) : NEW_LINE INDENT right_prod = [ 0 ] * N NEW_LINE flag = 0 NEW_LINE right_prod [ N - 1 ] = A [ N - 1 ] NEW_LINE for i in range ( N - 2 , 0 , - 1 ) : NEW_LINE INDENT right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] NEW_LINE DEDENT total_prod = right_prod [ 0 ] NEW_LINE product = 1 NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , N - 1 ) : NEW_LINE INDENT product *= A [ j ] NEW_LINE if ( gcd ( product , right_prod [ j + 1 ] ) == 1 or gcd ( product , total_prod / right_prod [ i ] ) == 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE print ( " ( " , ( i - 1 ) , " , ▁ " , ( j + 1 ) , " ) " ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE findPair ( arr , N ) NEW_LINE DEDENT