Minimum Sum of a pair at least K distance apart from an Array | Python3 program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Print the answer ; Driver Code
import sys NEW_LINE def findMinSum ( A , K ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE suffix_min = [ 0 ] * n ; NEW_LINE suffix_min [ n - 1 ] = A [ n - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix_min [ i ] = min ( suffix_min [ i + 1 ] , A [ i ] ) ; NEW_LINE DEDENT min_sum = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i + K < n ) : NEW_LINE INDENT min_sum = min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; NEW_LINE DEDENT DEDENT print ( min_sum ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE K = 2 ; NEW_LINE findMinSum ( A , K ) ; NEW_LINE DEDENT