Maximum rods to put horizontally such that no two rods overlap on X coordinate | Function to find the maximum number of rods that can be put horizontally ; Stores the result ; Stores the last occupied point ; Traverse the array arr [ ] ; If the current point can be put on the left side ; Increment the ans by 1 ; Update prev ; Else if the given point can be put on the right side ; Increment the ans by 1 ; Update prev ; Otherwise , ; Update prev ; Return the ans ; Driver Code
function findMaximumPoints ( N , X , H ) { let ans = 0 ; let prev = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; ++ i ) { if ( prev < ( X [ i ] - H [ i ] ) ) { ++ ans ; prev = X [ i ] ; } else if ( i == N - 1 || ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) { ++ ans ; prev = X [ i ] + H [ i ] ; } else { prev = X [ i ] ; } } ans ++ ; return ans ; } let X = [ 1 , 2 , 3 ] ; let H = [ 2 , 5 , 5 ] ; let N = X . length ; document . write ( findMaximumPoints ( N , X , H ) ) ;