Count numbers from a given range that can be visited moving any number of steps from the range [ L , R ] | Function to count points from the range [ X , Y ] that can be reached by moving by L or R steps ; Initialize difference array ; Initialize Count ; Marking starting point ; Iterating from X to Y ; Accumulate difference array ; If diff_arr [ i ] is greater than 1 ; Updating difference array ; Visited point found ; Driver Code ; Given Input ; Function Call
def countReachablePoints ( X , Y , L , R ) : NEW_LINE INDENT diff_arr = [ 0 for i in range ( 100000 ) ] NEW_LINE count = 0 NEW_LINE diff_arr [ X ] = 1 NEW_LINE diff_arr [ X + 1 ] = - 1 NEW_LINE for i in range ( X , Y + 1 , 1 ) : NEW_LINE INDENT diff_arr [ i ] += diff_arr [ i - 1 ] NEW_LINE if ( diff_arr [ i ] >= 1 ) : NEW_LINE INDENT diff_arr [ i + L ] += 1 NEW_LINE diff_arr [ i + R + 1 ] -= 1 NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 3 NEW_LINE Y = 12 NEW_LINE L = 2 NEW_LINE R = 3 NEW_LINE print ( countReachablePoints ( X , Y , L , R ) ) NEW_LINE DEDENT