Count number of ways to cover a distance | A Dynamic Programming based Java program to count number of ways ; Create the array of size 3. ; Initialize the bases cases ; Run a loop from 3 to n Bottom up approach to fill the array ; driver program
import java . util . * ; class GFG { static int printCountDP ( int dist ) { int [ ] ways = new int [ 3 ] ; int n = dist ; ways [ 0 ] = 1 ; ways [ 1 ] = 1 ; ways [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) ways [ i % 3 ] = ways [ ( i - 1 ) % 3 ] + ways [ ( i - 2 ) % 3 ] + ways [ ( i - 3 ) % 3 ] ; return ways [ n % 3 ] ; } public static void main ( String arg [ ] ) { int dist = 4 ; System . out . print ( printCountDP ( dist ) ) ; } }