- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we are at at (0, 0) position in the Cartesian plane. We want to go to the point (x, y) using only horizontal(H) and vertical(V) moves of single unit. There are more than one possible ways to reach destination. Each way comprises of few H moves and few V moves. (For example if we want to go to point (2,2) from point (0,0), then HVVH is one of the possible ways.) If we have another value k, we have to find the lexicographically kth smallest way of going to the destination.

So, if the input is like (x, y) = (3, 3) k = 3, then the output will be "HHVVVH"

To solve this, we will follow these steps −

- Define a function paths() . This will take x, y
- if min(x, y) < 0, then
- return 0

- return factorial(x+y)/factorial(x)/factorial(y)
- From the main method, do the following -
- res := a new list
- (p, q) := (0, 0)
- while (p, q) is not same as (x, y), do
- n := paths(x - p - 1, y - q)
- if p + 1 <= x and k < n, then
- insert 'H' at the end of res
- p := p + 1

- otherwise,
- k := k - n
- insert 'V' at the end of res
- q := q + 1

- return characters of res after joining

Let us see the following implementation to get better understanding −

from math import factorial def paths(x, y): if min(x, y) < 0: return 0 return factorial(x+y) / factorial(x) / factorial(y) def solve(x, y, k): res = [] p, q = 0, 0 while (p, q) != (x, y): n = paths(x - p - 1, y - q) if p + 1 <= x and k < n: res.append('H') p += 1 else: k -= n res.append('V') q += 1 return ''.join(res) (x, y) = (3, 3) k = 3 print(solve(x, y, k))

(3, 3), 3

HHVVVH

- Related Questions & Answers
- Program to find kth smallest n length lexicographically smallest string in python
- Program to find lexicographically smallest non-palindromic string in Python
- Program to find Lexicographically Smallest String With One Swap in Python
- Program to find lexicographically smallest string after applying operations in Python
- Program to find lexicographically smallest subsequence of size k in Python
- Program to find lexicographically smallest lowercase string of length k and distance n in Python
- 8085 program to move blocks of bits from source location to a destination location
- Lexicographically Smallest Equivalent String in C++
- Find the lexicographically smallest string which satisfies the given condition in Python
- Program to find lexicographically largest mountain list in Python
- Queries to answer the X-th smallest sub-string lexicographically in C++
- Program to find smallest string with a given numeric value in Python
- Concatenating n characters from source string to destination string in C
- Program to find nth smallest number from a given matrix in Python
- Python program to move spaces to front of string in single traversal

Advertisements