Minimum Sum of a pair at least K distance apart from an Array | 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 ; Print the answer ; Driver Code
function findMinSum ( A , K , n ) { let minimum_sum = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let mini = Number . MAX_VALUE ; for ( let j = i + K ; j < n ; j ++ ) mini = Math . min ( mini , A [ j ] ) ; if ( mini == Number . MAX_VALUE ) continue ; minimum_sum = Math . min ( minimum_sum , A [ i ] + mini ) ; } document . write ( minimum_sum ) ; } let A = [ 4 , 2 , 5 , 4 , 3 , 2 , 5 ] ; let K = 3 ; let n = A . length ; findMinSum ( A , K , n ) ;