Lexicographically Kth smallest way to reach given coordinate from origin
Last Updated :
07 Jul, 2022
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 strings starting strings will contains ‘H’ in the beginning i.e initial 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++
#include <bits/stdc++.h>
using namespace std;
int factorial( int a, int b)
{
int res = 1;
for ( int i = 1; i <= (a + b); i++)
res = res * i;
for ( int i = 1; i <= a; i++)
res = res / i;
for ( int i = 1; i <= b; i++)
res = res / i;
return res;
}
void Ksmallest( int x, int y, int k)
{
if (x == 0 && y == 0)
return ;
else if (x == 0) {
y--;
cout << "V" ;
Ksmallest(x, y, k);
}
else if (y == 0) {
x--;
cout << "H" ;
Ksmallest(x, y, k);
}
else {
if (factorial(x - 1, y) > k) {
cout << "H" ;
Ksmallest(x - 1, y, k);
}
else {
cout << "V" ;
Ksmallest(x, y - 1, k - factorial(x - 1, y));
}
}
}
int main()
{
int x = 2, y = 2, k = 2;
Ksmallest(x, y, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int factorial( int a,
int b)
{
int res = 1 ;
for ( int i = 1 ;
i <= (a + b); i++)
res = res * i;
for ( int i = 1 ; i <= a; i++)
res = res / i;
for ( int i = 1 ; i <= b; i++)
res = res / i;
return res;
}
static void Ksmallest( int x,
int y, int k)
{
if (x == 0 && y == 0 )
return ;
else if (x == 0 )
{
y--;
System.out.print( "V" );
Ksmallest(x, y, k);
}
else if (y == 0 )
{
x--;
System.out.print( "H" );
Ksmallest(x, y, k);
}
else
{
if (factorial(x - 1 , y) > k)
{
System.out.print( "H" );
Ksmallest(x - 1 , y, k);
}
else
{
System.out.print( "V" );
Ksmallest(x, y - 1 , k -
factorial(x - 1 , y));
}
}
}
public static void main (String[] args)
{
int x = 2 , y = 2 , k = 2 ;
Ksmallest(x, y, k);
}
}
|
Python3
def factorial(a, b):
res = 1
for i in range ( 1 , a + b + 1 ):
res = res * i
for i in range ( 1 , a + 1 ):
res = res / / i
for i in range ( 1 , b + 1 ):
res = res / / i
return res
def Ksmallest(x, y, k):
if x = = 0 and y = = 0 :
return
elif x = = 0 :
y - = 1
print ( "V" , end = "")
Ksmallest(x, y, k)
elif y = = 0 :
x - = 1
print ( "H" , end = "")
Ksmallest(x, y, k)
else :
if factorial(x - 1 , y) > k:
print ( "H" , end = "")
Ksmallest(x - 1 , y, k)
else :
print ( "V" , end = "")
Ksmallest(x, y - 1 , k - factorial(x - 1 , y))
if __name__ = = "__main__" :
x, y, k = 2 , 2 , 2
Ksmallest(x, y, k)
|
C#
using System;
class GFG
{
static int factorial( int a,
int b)
{
int res = 1;
for ( int i = 1;
i <= (a + b); i++)
res = res * i;
for ( int i = 1; i <= a; i++)
res = res / i;
for ( int i = 1; i <= b; i++)
res = res / i;
return res;
}
static void Ksmallest( int x,
int y, int k)
{
if (x == 0 && y == 0)
return ;
else if (x == 0)
{
y--;
Console.Write( "V" );
Ksmallest(x, y, k);
}
else if (y == 0)
{
x--;
Console.Write( "H" );
Ksmallest(x, y, k);
}
else
{
if (factorial(x - 1, y) > k)
{
Console.Write( "H" );
Ksmallest(x - 1, y, k);
}
else
{
Console.Write( "V" );
Ksmallest(x, y - 1, k -
factorial(x - 1, y));
}
}
}
public static void Main (String[] args)
{
int x = 2, y = 2, k = 2;
Ksmallest(x, y, k);
}
}
|
PHP
<?php
function factorial( $a , $b )
{
$res = 1;
for ( $i = 1; $i <= ( $a + $b ); $i ++)
$res = $res * $i ;
for ( $i = 1; $i <= $a ; $i ++)
$res = $res / $i ;
for ( $i = 1; $i <= $b ; $i ++)
$res = $res / $i ;
return $res ;
}
function Ksmallest( $x , $y , $k )
{
if ( $x == 0 && $y == 0)
return ;
else if ( $x == 0)
{
$y --;
echo ( "V" );
Ksmallest( $x , $y , $k );
}
else if ( $y == 0)
{
$x --;
echo ( "H" );
Ksmallest( $x , $y , $k );
}
else
{
if (factorial( $x - 1, $y ) > $k )
{
echo ( "H" );
Ksmallest( $x - 1, $y , $k );
}
else
{
echo ( "V" );
Ksmallest( $x , $y - 1, $k -
factorial( $x - 1, $y ));
}
}
}
$x = 2; $y = 2; $k = 2;
Ksmallest( $x , $y , $k );
?>
|
Javascript
<script>
function factorial(a, b)
{
var res = 1;
for ( var i = 1; i <= (a + b); i++)
res = res * i;
for ( var i = 1; i <= a; i++)
res = res / i;
for ( var i = 1; i <= b; i++)
res = res / i;
return res;
}
function Ksmallest(x, y, k)
{
if (x == 0 && y == 0)
return ;
else if (x == 0) {
y--;
document.write( "V" );
Ksmallest(x, y, k);
}
else if (y == 0) {
x--;
document.write( "H" );
Ksmallest(x, y, k);
}
else {
if (factorial(x - 1, y) > k) {
document.write( "H" );
Ksmallest(x - 1, y, k);
}
else {
document.write( "V" );
Ksmallest(x, y - 1, k - factorial(x - 1, y));
}
}
}
var x = 2, y = 2, k = 2;
Ksmallest(x, y, k);
</script>
|
Output
HVVH
Share your thoughts in the comments
Please Login to comment...