Number of ordered points pair satisfying line equation | Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c
function isValid ( arr , i , j , m , c ) { if ( i == j ) return false ; var lhs = arr [ j ] ; var rhs = m * arr [ i ] + c ; return lhs == rhs ; } function findOrderedPoints ( arr , n , m , c ) { var counter = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { var firstIndex = i , secondIndex = j ; if ( isValid ( arr , firstIndex , secondIndex , m , c ) ) counter ++ ; } } return counter ; } var arr = [ 1 , 2 , 3 , 4 , 2 ] ; var n = arr . length ; var m = 1 , c = 1 ; document . write ( findOrderedPoints ( arr , n , m , c ) ) ;