Lexicographically Kth smallest way to reach given coordinate from origin
Given a coordinate (x, y) on a 2D plane. We have to reach (x, y) from the current position which is at origin i.e (0, 0). In each step, we can either move vertically or horizontally on the plane. While moving horizontally each step we write ‘H’ and while moving vertically each step we write ‘V’. So, there can be possibly many strings containing ‘H’ and ‘V’ which represents a path from (0, 0) to (x, y). The task is to find the lexicographically Kth smallest string among all the possible strings.
Examples:
Input: x = 2, y = 2, k = 2
Output: HVVH
Explanation: There are 6 ways to reach (2, 2) from (0, 0). The possible list of strings in lexicographically sorted order: [“HHVV”, “HVHV”, “HVVH”, “VHHV”, “VHVH”, “VVHH”]. Hence, the lexicographically 2nd smallest string is HVHV.Input : x = 2, y = 2, k = 3
Output : VHHV
Prerequisites: Ways to Reach a Point from Origin
Approach: The idea is to use recursion to solve the problem. Number of ways to reach (x, y) from origin is x + yCx.
Now observe, the number of ways to reach (x, y) from (1, 0) will be (x + y – 1, x – 1) because we have already made a step in the horizontal direction, so 1 is subtracted from x. Also, the number of ways to reach (x, y) from (0, 1) will be (x + y – 1, y – 1) because we have already made a step in the vertical direction, so 1 is subtracted from y. Since ‘H’ is lexicographically smaller than ‘V’, so among all stringsa starting strings will contains ‘H’ in the beginning i.e inital movements will be Horizontal.
So, if K <= x + y – 1Cx – 1, we will take ‘H’ as first step else we will take ‘V’ as first step and solve for number of goings to (x, y) from(1, 0) will be K = K – x + y – 1Cx – 1.
Below is the implementation of this approach:
C++
// CPP Program to find Lexicographically Kth // smallest way to reach given coordinate from origin #include <bits/stdc++.h> using namespace std; // Return (a+b)!/a!b! int factorial( int a, int b) { int res = 1; // finding (a+b)! for ( int i = 1; i <= (a + b); i++) res = res * i; // finding (a+b)!/a! for ( int i = 1; i <= a; i++) res = res / i; // finding (a+b)!/b! for ( int i = 1; i <= b; i++) res = res / i; return res; } // Return the Kth smallest way to reach given coordinate from origin void Ksmallest( int x, int y, int k) { // if at origin if (x == 0 && y == 0) return ; // if on y-axis else if (x == 0) { // decrement y. y--; // Move vertical cout << "V" ; // recursive call to take next step. Ksmallest(x, y, k); } // If on x-axis else if (y == 0) { // decrement x. x--; // Move horizontal. cout << "H" ; // recursive call to take next step. Ksmallest(x, y, k); } else { // If x + y C x is greater than K if (factorial(x - 1, y) > k) { // Move Horizontal cout << "H" ; // recursive call to take next step. Ksmallest(x - 1, y, k); } else { // Move vertical cout << "V" ; // recursive call to take next step. Ksmallest(x, y - 1, k - factorial(x - 1, y)); } } } // Driven Program int main() { int x = 2, y = 2, k = 2; Ksmallest(x, y, k); return 0; } |
Java
// Java Program to find // Lexicographically Kth // smallest way to reach // given coordinate from origin import java.io.*; class GFG { // Return (a+b)!/a!b! static int factorial( int a, int b) { int res = 1 ; // finding (a+b)! for ( int i = 1 ; i <= (a + b); i++) res = res * i; // finding (a+b)!/a! for ( int i = 1 ; i <= a; i++) res = res / i; // finding (a+b)!/b! for ( int i = 1 ; i <= b; i++) res = res / i; return res; } // Return the Kth smallest // way to reach given // coordinate from origin static void Ksmallest( int x, int y, int k) { // if at origin if (x == 0 && y == 0 ) return ; // if on y-axis else if (x == 0 ) { // decrement y. y--; // Move vertical System.out.print( "V" ); // recursive call to // take next step. Ksmallest(x, y, k); } // If on x-axis else if (y == 0 ) { // decrement x. x--; // Move horizontal. System.out.print( "H" ); // recursive call to // take next step. Ksmallest(x, y, k); } else { // If x + y C x is // greater than K if (factorial(x - 1 , y) > k) { // Move Horizontal System.out.print( "H" ); // recursive call to // take next step. Ksmallest(x - 1 , y, k); } else { // Move vertical System.out.print( "V" ); // recursive call to // take next step. Ksmallest(x, y - 1 , k - factorial(x - 1 , y)); } } } // Driver Code public static void main (String[] args) { int x = 2 , y = 2 , k = 2 ; Ksmallest(x, y, k); } } // This code is contributed // by anuj_67. |
C#
// C# Program to find // Lexicographically Kth // smallest way to reach // given coordinate from origin using System; class GFG { // Return (a+b)!/a!b! static int factorial( int a, int b) { int res = 1; // finding (a+b)! for ( int i = 1; i <= (a + b); i++) res = res * i; // finding (a+b)!/a! for ( int i = 1; i <= a; i++) res = res / i; // finding (a+b)!/b! for ( int i = 1; i <= b; i++) res = res / i; return res; } // Return the Kth smallest // way to reach given // coordinate from origin static void Ksmallest( int x, int y, int k) { // if at origin if (x == 0 && y == 0) return ; // if on y-axis else if (x == 0) { // decrement y. y--; // Move vertical Console.Write( "V" ); // recursive call to // take next step. Ksmallest(x, y, k); } // If on x-axis else if (y == 0) { // decrement x. x--; // Move horizontal. Console.Write( "H" ); // recursive call to // take next step. Ksmallest(x, y, k); } else { // If x + y C x is // greater than K if (factorial(x - 1, y) > k) { // Move Horizontal Console.Write( "H" ); // recursive call to // take next step. Ksmallest(x - 1, y, k); } else { // Move vertical Console.Write( "V" ); // recursive call to // take next step. Ksmallest(x, y - 1, k - factorial(x - 1, y)); } } } // Driver Code public static void Main (String[] args) { int x = 2, y = 2, k = 2; Ksmallest(x, y, k); } } // This code is contributed by 29AjayKumar |
Output
HVVH
Recommended Posts:
- Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- Construct lexicographically smallest palindrome
- Lexicographically smallest rotated sequence | Set 2
- Lexicographically smallest array after at-most K consecutive swaps
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest permutation of {1, .. n} such that no. and position do not match
- Make a lexicographically smallest palindrome with minimal changes
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest permutation with no digits at Original Index
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Finding Quadrant of a Coordinate with respect to a Circle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.