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 ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Prthe answer ; Driver Code
import sys NEW_LINE def findMinSum ( A , K ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE minimum_sum = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT minimum = sys . maxsize ; NEW_LINE for j in range ( i + K , n , 1 ) : NEW_LINE INDENT minimum = min ( minimum , A [ j ] ) ; NEW_LINE DEDENT if ( minimum == sys . maxsize ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT minimum_sum = min ( minimum_sum , A [ i ] + minimum ) ; NEW_LINE DEDENT print ( minimum_sum ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 2 , 5 , 4 , 3 , 2 , 5 ] ; NEW_LINE K = 3 ; NEW_LINE findMinSum ( A , K ) ; NEW_LINE DEDENT