Maximum rods to put horizontally such that no two rods overlap on X coordinate | Python 3 program for the above approach ; 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
import sys NEW_LINE def findMaximumPoints ( N , X , H ) : NEW_LINE INDENT ans = 0 NEW_LINE prev = - sys . maxsize - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( prev < ( X [ i ] - H [ i ] ) ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = X [ i ] NEW_LINE DEDENT elif ( i == N - 1 or ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE prev = X [ i ] + H [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT prev = X [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 1 , 2 , 3 ] NEW_LINE H = [ 2 , 5 , 5 ] NEW_LINE N = len ( X ) NEW_LINE print ( findMaximumPoints ( N , X , H ) ) NEW_LINE DEDENT