Count ways to reach the Nth stair using any step from the given array
Given N stairs and a person standing at the bottom wants to reach the top. He could climb any number of steps from the given array arr[] of positive integers. The task is to find the count of all possible ways to reach the top.
Examples:
Input: arr[] = {1, 3, 5}, N = 5
Output: 5
(0 -> 1 -> 2 -> 3 -> 4 -> 5), (0 -> 1 -> 2 -> 5),
(0 -> 1 -> 4 -> 5), (0 -> 3 -> 4 -> 5)
and (0 -> 5) are the only possible ways.
Input: arr[] = {1, 2, 3}, N = 10
Output: 274
Naive approach: Using recursion, find all the possible ways and count them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int n, int arr[], int len)
{
if (n == 0)
return 1;
int no_ways = 0;
for ( int i = 0; i < len; i++) {
if (n - arr[i] >= 0) {
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
int main()
{
int arr[] = { 1, 3, 5 };
int len = sizeof (arr) / sizeof ( int );
int n = 5;
cout << countWays(n, arr, len);
return 0;
}
|
Java
class GFG {
static int countWays( int n, int arr[], int len)
{
if (n == 0 )
return 1 ;
int no_ways = 0 ;
for ( int i = 0 ; i < len; i++) {
if (n - arr[i] >= 0 ) {
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
public static void main(String args[])
{
int arr[] = { 1 , 3 , 5 };
int len = arr.length;
;
int n = 5 ;
System.out.println(countWays(n, arr, len));
}
}
|
Python
def countWays(n, arr):
if (n = = 0 ):
return 1
no_ways = 0
for i in arr:
if (n - i > = 0 ):
no_ways = no_ways + countWays(n - i, arr)
return no_ways
arr = [ 1 , 3 , 5 ]
n = 5
print (countWays(n, arr))
|
C#
using System;
class GFG
{
static int countWays( int n, int []arr,
int len)
{
if (n == 0)
return 1;
int no_ways = 0;
for ( int i = 0; i < len; i++)
{
if (n - arr[i] >= 0)
{
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
public static void Main()
{
int []arr = { 1, 3, 5 };
int len = arr.Length;
int n = 5;
Console.Write(countWays(n, arr, len));
}
}
|
Javascript
<script>
function countWays(n, arr, len) {
if (n == 0)
return 1;
let no_ways = 0;
for (let i = 0; i < len; i++) {
if (n - arr[i] >= 0) {
no_ways += countWays(n - arr[i], arr, len);
}
}
return no_ways;
}
let arr = [1, 3, 5];
let len = arr.length;
let n = 5;
document.write(countWays(n, arr, len));
</script>
|
Efficient approach: In this method instead of using a recursive approach, go for a dynamic programming based approach.
- Create an array count[] of size equal to the total number of steps + 1 with all elements initialized to 0 and initialize the first element i.e. count[0] to 1.
- Initialize a variable no_ways = 0 inside the for loop and every time starting from 0 for the new ways of climbing the stairs.
- Add count[i – x[j]] to no_ways only if i – x[j] ≥ 0.
- Finally, return count[N] which is essentially the number of ways to climb the Nth stair.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int n, int arr[], int len)
{
int count[n + 1] = { 0 };
count[0] = 1;
if (n == 0)
return 1;
for ( int i = 1; i <= n; i++) {
int no_ways = 0;
for ( int j = 0; j < len; j++) {
if (i - arr[j] >= 0) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
int main()
{
int arr[] = { 1, 3, 5 };
int len = sizeof (arr) / sizeof ( int );
int n = 5;
cout << countWays(n, arr, len);
return 0;
}
|
Java
class GFG {
static int countWays( int n, int arr[], int len)
{
int count[] = new int [n + 1 ];
count[ 0 ] = 1 ;
if (n == 0 )
return 1 ;
for ( int i = 1 ; i <= n; i++) {
int no_ways = 0 ;
for ( int j = 0 ; j < len; j++) {
if (i - arr[j] >= 0 ) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
public static void main(String args[])
{
int arr[] = { 1 , 3 , 5 };
int len = arr.length;
int n = 5 ;
System.out.print(countWays(n, arr, len));
}
}
|
Python3
def countWays(n, arr):
count = [ 0 ] * (n + 1 )
count[ 0 ] = 1
if (n = = 0 ):
return 1
for i in range ( 1 , n + 1 ):
no_ways = 0
for j in arr:
if (i - j > = 0 ):
no_ways + = count[i - j]
count[i] = no_ways
return count[n]
arr = [ 1 , 3 , 5 ]
n = 5
print (countWays(n, arr))
|
C#
using System;
class GFG
{
static int countWays( int n, int []arr, int len)
{
int []count = new int [n + 1];
count[0] = 1;
if (n == 0)
return 1;
for ( int i = 1; i <= n; i++)
{
int no_ways = 0;
for ( int j = 0; j < len; j++)
{
if (i - arr[j] >= 0)
{
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
public static void Main()
{
int []arr = { 1, 3, 5 };
int len = arr.Length;
int n = 5;
Console.WriteLine(countWays(n, arr, len));
}
}
|
Javascript
<script>
function countWays(n, arr, len)
{
let count = new Array(n + 1);
count[0] = 1;
if (n == 0)
return 1;
for (let i = 1; i <= n; i++) {
let no_ways = 0;
for (let j = 0; j < len; j++) {
if (i - arr[j] >= 0) {
no_ways += count[i - arr[j]];
}
count[i] = no_ways;
}
}
return count[n];
}
let arr = [ 1, 3, 5 ];
let len = arr.length;
let n = 5;
document.write(countWays(n, arr, len));
</script>
|
Time Complexity: O(N * len) where N is the number of stairs and len is the length of the array.
Auxiliary Space: O(len), where len is the length of the array.
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...