Path traversed using exactly M coins in K jumps | Function that print the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver Code ; Function Call
function print_path ( N , jump , coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { document . write ( " " ) ; } else { let pos = 1 ; while ( jump > 0 ) { let tmp = Math . min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } document . write ( pos + " " ) ; ; coin -= tmp ; jump -= 1 ; } } } let N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ;