Open In App

Lexicographically Kth smallest way to reach given coordinate from origin

Last Updated : 07 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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.


Python3




# Python3 Program to find Lexicographically Kth
# smallest way to reach given coordinate from origin
 
# Return (a+b)!/a!b!
def factorial(a, b):
 
    res = 1
 
    # finding (a+b)!
    for i in range(1, a + b + 1):
        res = res * i
 
    # finding (a+b)!/a!
    for i in range(1, a + 1):
        res = res // i
 
    # finding (a+b)!/b!
    for i in range(1, b + 1):
        res = res // i
 
    return res
 
# Return the Kth smallest way to reach
# given coordinate from origin
def Ksmallest(x, y, k):
 
    # if at origin
    if x == 0 and y == 0:
        return
 
    # if on y-axis
    elif x == 0:
        # decrement y.
        y -= 1
 
        # Move vertical
        print("V", end = "")
 
        # recursive call to take next step.
        Ksmallest(x, y, k)
     
    # If on x-axis
    elif y == 0:
         
        # decrement x.
        x -= 1
 
        # Move horizontal.
        print("H", end = "")
 
        # 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
            print("H", end = "")
 
            # recursive call to take next step.
            Ksmallest(x - 1, y, k)
         
        else:
             
            # Move vertical
            print("V", end = "")
 
            # recursive call to take next step.
            Ksmallest(x, y - 1, k - factorial(x - 1, y))
         
# Driver Code
if __name__ == "__main__":
 
    x, y, k = 2, 2, 2
    Ksmallest(x, y, k)
 
# This code is contributed by Rituraj Jain


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


PHP




<?php
// PHP Program to find Lexicographically Kth
// smallest way to reach given coordinate from origin
 
// Return (a+b)!/a!b!
function factorial($a, $b)
{
    $res = 1;
 
    // finding (a+b)!
    for ($i = 1; $i <= ($a + $b); $i++)
        $res = $res * $i;
 
    // finding (a+b)!/a!
    for ($i = 1; $i <= $a; $i++)
        $res = $res / $i;
 
    // finding (a+b)!/b!
    for ($i = 1; $i <= $b; $i++)
        $res = $res / $i;
 
    return $res;
}
 
// Return the Kth smallest way to reach
// given coordinate from origin
function Ksmallest($x, $y, $k)
{
    // if at origin
    if ($x == 0 && $y == 0)
        return;
 
    // if on y-axis
    else if ($x == 0)
    {
        // decrement y.
        $y--;
 
        // Move vertical
        echo("V");
 
        // recursive call to
        // take next step.
        Ksmallest($x, $y, $k);
    }
 
    // If on x-axis
    else if ($y == 0)
    {
        // decrement x.
        $x--;
 
        // Move horizontal.
        echo("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
            echo("H");
 
            // recursive call to
            // take next step.
            Ksmallest($x - 1, $y, $k);
        }
        else
        {
            // Move vertical
            echo("V");
 
            // recursive call to
            // take next step.
            Ksmallest($x, $y - 1, $k -
            factorial($x - 1, $y));
        }
    }
}
 
// Driver Code
$x = 2; $y = 2;$k = 2;
 
Ksmallest($x, $y, $k);
 
// This code is contributed
// by Code_Mech.
?>


Javascript




<script>
 
// Javascript Program to find Lexicographically Kth
// smallest way to reach given coordinate from origin
 
// Return (a+b)!/a!b!
function factorial(a, b)
{
    var res = 1;
 
    // finding (a+b)!
    for (var i = 1; i <= (a + b); i++)
        res = res * i;
 
    // finding (a+b)!/a!
    for (var i = 1; i <= a; i++)
        res = res / i;
 
    // finding (a+b)!/b!
    for (var i = 1; i <= b; i++)
        res = res / i;
 
    return res;
}
 
// Return the Kth smallest way to reach given coordinate from origin
function Ksmallest(x, y, k)
{
    // if at origin
    if (x == 0 && y == 0)
        return;
 
    // if on y-axis
    else if (x == 0) {
        // decrement y.
        y--;
 
        // Move vertical
        document.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.
        document.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
            document.write( "H");
 
            // recursive call to take next step.
            Ksmallest(x - 1, y, k);
        }
        else {
            // Move vertical
            document.write( "V");
 
            // recursive call to take next step.
            Ksmallest(x, y - 1, k - factorial(x - 1, y));
        }
    }
}
 
// Driven Program
var x = 2, y = 2, k = 2;
Ksmallest(x, y, k);
 
// This code is contributed by rutvik_56.
</script>


Output 
 

HVVH

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads