Count all disjoint pairs having absolute difference at least K from a given array | ''Function to count distinct pairs with absolute difference atleast K ; '' Track the element that  have been paired ; '' Stores count of distinct pairs ; '' Pick all elements one by one ; '' If already visited ; '' If already visited ; '' If difference is at least K ; '' Mark element as visited and  increment the count ; '' Print the final count ; ''Driver Code ; '' Given arr[] ; '' Size of array ; '' Given difference K ; '' Function Call
def countPairsWithDiffK ( arr , N , K ) : NEW_LINE INDENT vis = [ 0 ] * N NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( vis [ i ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( vis [ j ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( abs ( arr [ i ] - arr [ j ] ) >= K ) : NEW_LINE INDENT count += 1 NEW_LINE vis [ i ] = 1 NEW_LINE vis [ j ] = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE countPairsWithDiffK ( arr , N , K ) NEW_LINE DEDENT