Count numbers from a given range that can be visited moving any number of steps from the range [ L , R ] | Java code for above approach ; 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
import java . util . * ; class GFG { static int countReachablePoints ( int X , int Y , int L , int R ) { int diff_arr [ ] = new int [ 100000 ] ; int count = 0 ; diff_arr [ X ] = 1 ; diff_arr [ X + 1 ] = - 1 ; for ( int i = X ; i <= Y ; i ++ ) { diff_arr [ i ] += diff_arr [ i - 1 ] ; if ( diff_arr [ i ] >= 1 ) { diff_arr [ i + L ] += 1 ; diff_arr [ i + R + 1 ] -= 1 ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { int X = 3 , Y = 12 , L = 2 , R = 3 ; System . out . print ( countReachablePoints ( X , Y , L , R ) ) ; } }