# Count numbers from a given range that can be visited moving any number of steps from the range [L, R]

Given two integers X, Y and a range [L, R], the task is to count the number of integers from the range X and Y (inclusive) that can be visited in any number of steps, starting from X. In each step, it is possible to increase by L to R.

Examples:

Input: X = 1, Y = 10, L = 4, R = 6
Output: 6
Explanation: A total of six points can be visited between [1, 10]:

1. 1: Starting point
2. 5: 1 -> 5
3. 6: 1 -> 6
4. 7: 1 -> 7
5. 9: 1 -> 5 -> 9
6. 10: 1 -> 5 -> 10

Input: X = 3, Y = 12, L = 2, R = 3
Output: 9

Approach: From index i, one can reach anywhere between [i+L, i+R] also similarly for each point j in [i+L, i+R] one can move to [j+L, j+R]. For each index i these reachable ranges can be marked using the difference array. Follow the steps below for the approach.

• Construct an array say diff_arr[] of large size, to mark the ranges.
• Initialize a variable say count = 0, to count the reachable points.
• Initially, make diff_arr[x] = 1 and diff_arr[x+1] = -1 to mark starting point X as visited.
• Iterate from X to Y and for each i update diff_arr[i] += diff_arr[i-1]  and if diff_arr[i] >= 1 then update diff_arr[i+L] = 1 and diff_arr[i + R + 1] = -1 and count = count + 1.
• Finally, return the count.

Below is the implementation of the above approach:

## C++

 `// C++ code for above approach` `#include ` `using` `namespace` `std;`   `// Function to count points from the range [X, Y]` `// that can be reached by moving by L or R steps` `int` `countReachablePoints(``int` `X, ``int` `Y,` `                         ``int` `L, ``int` `R)` `{`   `    ``// Initialize difference array` `    ``int` `diff_arr = { 0 };`   `    ``// Initialize Count` `    ``int` `count = 0;`   `    ``// Marking starting point` `    ``diff_arr[X] = 1;` `    ``diff_arr[X + 1] = -1;`   `    ``// Iterating from X to Y` `    ``for` `(``int` `i = X; i <= Y; i++) {`   `        ``// Accumulate difference array` `        ``diff_arr[i] += diff_arr[i - 1];`   `        ``// If diff_arr[i] is greater` `        ``// than 1` `        ``if` `(diff_arr[i] >= 1) {` `            ``// Updating difference array` `            ``diff_arr[i + L] += 1;` `            ``diff_arr[i + R + 1] -= 1;`   `            ``// Visited point found` `            ``count++;` `        ``}` `    ``}` `    ``return` `count;` `}`   `// Driver Code` `int` `main()`   `{` `    ``// Given Input` `    ``int` `X = 3, Y = 12, L = 2, R = 3;`   `    ``// Function Call` `    ``cout << countReachablePoints(X, Y, L, R);`   `    ``return` `0;` `}`

## Java

 `// Java code for above approach`   `import` `java.util.*;`   `class` `GFG{`   `// Function to count points from the range [X, Y]` `// that can be reached by moving by L or R steps` `static` `int` `countReachablePoints(``int` `X, ``int` `Y,` `                         ``int` `L, ``int` `R)` `{`   `    ``// Initialize difference array` `    ``int` `diff_arr[] = ``new` `int``[``100000``];`   `    ``// Initialize Count` `    ``int` `count = ``0``;`   `    ``// Marking starting point` `    ``diff_arr[X] = ``1``;` `    ``diff_arr[X + ``1``] = -``1``;`   `    ``// Iterating from X to Y` `    ``for` `(``int` `i = X; i <= Y; i++) {`   `        ``// Accumulate difference array` `        ``diff_arr[i] += diff_arr[i - ``1``];`   `        ``// If diff_arr[i] is greater` `        ``// than 1` `        ``if` `(diff_arr[i] >= ``1``) {` `            ``// Updating difference array` `            ``diff_arr[i + L] += ``1``;` `            ``diff_arr[i + R + ``1``] -= ``1``;`   `            ``// Visited point found` `            ``count++;` `        ``}` `    ``}` `    ``return` `count;` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)`   `{` `    ``// Given Input` `    ``int` `X = ``3``, Y = ``12``, L = ``2``, R = ``3``;`   `    ``// Function Call` `    ``System.out.print(countReachablePoints(X, Y, L, R));`   `}` `}`   `// This code contributed by shikhasingrajput`

## Python3

 `# Python3 code for above approach`   `# Function to count points from the range [X, Y]` `# that can be reached by moving by L or R steps` `def` `countReachablePoints(X, Y, L, R):` `    `  `    ``# Initialize difference array` `    ``diff_arr ``=` `[``0` `for` `i ``in` `range``(``100000``)]`   `    ``# Initialize Count` `    ``count ``=` `0`   `    ``# Marking starting point` `    ``diff_arr[X] ``=` `1` `    ``diff_arr[X ``+` `1``] ``=` `-``1`   `    ``# Iterating from X to Y` `    ``for` `i ``in` `range``(X, Y ``+` `1``, ``1``):` `        `  `        ``# Accumulate difference array` `        ``diff_arr[i] ``+``=` `diff_arr[i ``-` `1``]`   `        ``# If diff_arr[i] is greater` `        ``# than 1` `        ``if` `(diff_arr[i] >``=` `1``):` `            `  `            ``# Updating difference array` `            ``diff_arr[i ``+` `L] ``+``=` `1` `            ``diff_arr[i ``+` `R ``+` `1``] ``-``=` `1`   `            ``# Visited point found` `            ``count ``+``=` `1`   `    ``return` `count`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``# Given Input` `    ``X ``=` `3` `    ``Y ``=` `12` `    ``L ``=` `2` `    ``R ``=` `3`   `    ``# Function Call` `    ``print``(countReachablePoints(X, Y, L, R))` `    `  `# This code is contributed by ipg2016107`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG{`   `// Function to count points from the range [X, Y]` `// that can be reached by moving by L or R steps` `static` `int` `countReachablePoints(``int` `X, ``int` `Y,` `                         ``int` `L, ``int` `R)` `{`   `    ``// Initialize difference array` `    ``int``[] diff_arr= ``new` `int``;`   `    ``// Initialize Count` `    ``int` `count = 0;`   `    ``// Marking starting point` `    ``diff_arr[X] = 1;` `    ``diff_arr[X + 1] = -1;`   `    ``// Iterating from X to Y` `    ``for` `(``int` `i = X; i <= Y; i++) {`   `        ``// Accumulate difference array` `        ``diff_arr[i] += diff_arr[i - 1];`   `        ``// If diff_arr[i] is greater` `        ``// than 1` `        ``if` `(diff_arr[i] >= 1)` `        ``{` `          `  `            ``// Updating difference array` `            ``diff_arr[i + L] += 1;` `            ``diff_arr[i + R + 1] -= 1;`   `            ``// Visited point found` `            ``count++;` `        ``}` `    ``}` `    ``return` `count;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` `        ``// Given Input` `    ``int` `X = 3, Y = 12, L = 2, R = 3;`   `    ``// Function Call` `    ``Console.Write(countReachablePoints(X, Y, L, R));` `}` `}`   `// This code is contributed by splevel62.`

## Javascript

 ``

Output:

`9`

Time Complexity: O(Y – X)
Auxiliary Space: O(Y)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next