Minimum Numbers of cells that are connected with the smallest path between 3 given cells | Function to return the minimum cells that are connected via the minimum length path ; Array to store column number of the given cells ; Sort cells in ascending order of row number ; Middle row number ; Set pair to store required cells ; Range of column number ; Store all cells of middle row within column number range ; Final step to store all the column number of 1 st and 3 rd cell upto MidRow ; vector pair to store X , Y , Z
function Minimum_Cells ( v ) { let col = new Array ( 3 ) ; let i , j ; for ( i = 0 ; i < 3 ; i ++ ) { let column_number = v [ i ] [ 1 ] ; col [ i ] = column_number ; } col . sort ( function ( a , b ) { return a - b } ) ; v . sort ( ) ; let MidRow = v [ 1 ] [ 0 ] ; let s = new Set ( ) ; let Maxcol = col [ 2 ] , MinCol = col [ 0 ] ; for ( i = MinCol ; i <= Maxcol ; i ++ ) { s . add ( [ MidRow , i ] ) ; } for ( i = 0 ; i < 3 ; i ++ ) { if ( v [ i ] [ 0 ] == MidRow ) continue ; for ( j = Math . min ( v [ i ] [ 0 ] , MidRow ) ; j <= Math . max ( v [ i ] [ 0 ] , MidRow ) ; j ++ ) { s . add ( [ j , v [ i ] [ 0 ] ] ) ; } } return s . size - 2 ; } let v = [ ] ; v . push ( [ 0 , 0 ] ) ; v . push ( [ 1 , 1 ] ) ; v . push ( [ 2 , 2 ] ) ; document . write ( Minimum_Cells ( v ) ) ;