Given an integer N, representing the number of stairs, valued from 1 to N, and a starting position S, the task is to count the maximum number of unique stairs that can be reached by moving exactly A or B stairs steps forward or backward from any position, any number of times.
Examples:
Input: N = 10, S = 2, A = 5, B = 7
Output: 4
Explanation:
Starting Position S: Stair 2
From Stair 2, it is possible to reach the stairs 7, i.e. ( S + A ), and 9, i.e. (S + B).
From Stair 9, it is possible to reach stair 4, i.e. ( S – A ).
Therefore, the unique number of stairs that can be reached is 4 {2, 4, 7, 9}.
Input: N = 10, S = 2, A = 3, B = 4
Output: 10
Approach: The given problem can be solved using BFS traversal technique. Follow the steps below to solve the problem:
- Initialize a queue and an array vis[] of size (N + 1) and initialize it as false.
- Mark the starting node S as visited i.e., vis[S] as 1. Push S into a queue Q.
- Now iterate until Q is not empty and perform the following steps:
- Pop the front element of the queue and store it in a variable, say currStair.
- Consider all 4 possible types of moves from currStair, i.e. {+A, -A, +B, -B}.
- For every new stair, check whether it is a valid and unvisited stair or not. If found to be true, then push it into Q. Mark the stair as visited. Otherwise, continue.
- Finally, count the number of visited stairs using the array vis[].
- After completing the above steps, print the count of visited stairs as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countStairs( int n, int x, int a, int b)
{
int vis[n + 1] = { 0 };
int moves[] = { +a, -a, +b, -b };
queue< int > q;
q.push(x);
vis[x] = 1;
while (!q.empty()) {
int currentStair = q.front();
q.pop();
for ( int j = 0; j < 4; j++) {
int newStair = currentStair + moves[j];
if (newStair > 0 && newStair <= n
&& !vis[newStair]) {
q.push(newStair);
vis[newStair] = 1;
}
}
}
int cnt = 0;
for ( int i = 1; i <= n; i++)
if (vis[i] == 1)
cnt++;
cout << cnt;
}
int main()
{
int N = 10, S = 2, A = 5, B = 7;
countStairs(N, S, A, B);
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
class GFG{
static void countStairs( int n, int x, int a, int b)
{
int [] vis = new int [n + 1 ];
int [] moves = { +a, -a, +b, -b };
Queue<Integer> q = new LinkedList<Integer>();
q.add(x);
vis[x] = 1 ;
while (!q.isEmpty())
{
int currentStair = q.peek();
q.remove();
for ( int j = 0 ; j < 4 ; j++)
{
int newStair = currentStair + moves[j];
if (newStair > 0 && newStair <= n &&
vis[newStair] == 0 )
{
q.add(newStair);
vis[newStair] = 1 ;
}
}
}
int cnt = 0 ;
for ( int i = 1 ; i <= n; i++)
if (vis[i] == 1 )
cnt++;
System.out.print(cnt);
}
public static void main(String args[])
{
int N = 10 , S = 2 , A = 5 , B = 7 ;
countStairs(N, S, A, B);
}
}
|
Python3
from collections import deque
def countStairs(n, x, a, b):
vis = [ 0 ] * (n + 1 )
moves = [ + a, - a, + b, - b]
q = deque()
q.append(x)
vis[x] = 1
while ( len (q) > 0 ):
currentStair = q.popleft()
for j in range ( 4 ):
newStair = currentStair + moves[j]
if (newStair > 0 and newStair < = n and
( not vis[newStair])):
q.append(newStair)
vis[newStair] = 1
cnt = 0
for i in range ( 1 , n + 1 ):
if (vis[i] = = 1 ):
cnt + = 1
print (cnt)
if __name__ = = '__main__' :
N, S, A, B = 10 , 2 , 5 , 7
countStairs(N, S, A, B)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countStairs( int n, int x,
int a, int b)
{
int []vis = new int [n + 1];
Array.Clear(vis, 0, vis.Length);
int []moves = { +a, -a, +b, -b };
Queue< int > q = new Queue< int >();
/// Push the starting position
q.Enqueue(x);
vis[x] = 1;
while (q.Count > 0)
{
int currentStair = q.Peek();
q.Dequeue();
for ( int j = 0; j < 4; j++)
{
int newStair = currentStair + moves[j];
if (newStair > 0 && newStair <= n &&
vis[newStair] == 0)
{
q.Enqueue(newStair);
vis[newStair] = 1;
}
}
}
int cnt = 0;
for ( int i = 1; i <= n; i++)
if (vis[i] == 1)
cnt++;
Console.WriteLine(cnt);
}
public static void Main()
{
int N = 10, S = 2, A = 5, B = 7;
countStairs(N, S, A, B);
}
}
|
Javascript
<script>
function countStairs(n, x, a, b)
{
var vis = Array(n+1).fill(0);
var moves = [ +a, -a, +b, -b ];
var q = [];
q.push(x);
vis[x] = 1;
while (q.length!=0) {
var currentStair = q[0];
q.shift();
for ( var j = 0; j < 4; j++) {
var newStair = currentStair + moves[j];
if (newStair > 0 && newStair <= n
&& !vis[newStair]) {
q.push(newStair);
vis[newStair] = 1;
}
}
}
var cnt = 0;
for ( var i = 1; i <= n; i++)
if (vis[i] == 1)
cnt++;
document.write( cnt);
}
var N = 10, S = 2, A = 5, B = 7;
countStairs(N, S, A, B);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
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!
Last Updated :
05 Oct, 2021
Like Article
Save Article