Count numbers from a given range that can be visited moving any number of steps from the range [L, R]
Last Updated :
05 Aug, 2021
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: Starting point
- 5: 1 -> 5
- 6: 1 -> 6
- 7: 1 -> 7
- 9: 1 -> 5 -> 9
- 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++
#include <bits/stdc++.h>
using namespace std;
int countReachablePoints( int X, int Y,
int L, int R)
{
int diff_arr[100000] = { 0 };
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;
}
int main()
{
int X = 3, Y = 12, L = 2, R = 3;
cout << countReachablePoints(X, Y, L, R);
return 0;
}
|
Java
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));
}
}
|
Python3
def countReachablePoints(X, Y, L, R):
diff_arr = [ 0 for i in range ( 100000 )]
count = 0
diff_arr[X] = 1
diff_arr[X + 1 ] = - 1
for i in range (X, Y + 1 , 1 ):
diff_arr[i] + = diff_arr[i - 1 ]
if (diff_arr[i] > = 1 ):
diff_arr[i + L] + = 1
diff_arr[i + R + 1 ] - = 1
count + = 1
return count
if __name__ = = '__main__' :
X = 3
Y = 12
L = 2
R = 3
print (countReachablePoints(X, Y, L, R))
|
C#
using System;
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()
{
int X = 3, Y = 12, L = 2, R = 3;
Console.Write(countReachablePoints(X, Y, L, R));
}
}
|
Javascript
<script>
function countReachablePoints(X, Y, L, R) {
let diff_arr = new Array(100000).fill(0);
let count = 0;
diff_arr[X] = 1;
diff_arr[X + 1] = -1;
for (let 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;
}
let X = 3,
Y = 12,
L = 2,
R = 3;
document.write(countReachablePoints(X, Y, L, R));
</script>
|
Time Complexity: O(Y – X)
Auxiliary Space: O(Y)
Share your thoughts in the comments
Please Login to comment...