Open In App

Find the k-th string in lexicographical order consisting of n-2 X’s and 2 Y’s

Given two numbers N and K, the task is to find the Kth string in lexicographical order if the starting string contains (N-2) x’s first and then 2 Y’s
Note: 
 

1 ? K ? N*(N-1)/2, N*(N-1)/2 are the number of possible permutations 
 



Examples: 
 

Input : N = 5, K = 7 
Output : YXXXY 
The possible strings in lexicographical order 
1. XXXYY 
2. XXYXY 
3. XXYYX 
4. XYXXY 
5. XYXYX 
6. XYYXX 
7. YXXXY 
8. YXXYX 
9. YXYXX 
10. YYXXX
Input : N = 8, K = 20 
Output : XYXYXXXX 
 



 

Approach
Inorder to find the kth position string, we have to follow the following steps- 
 

  1. we have to find the position of the leftmost occurrence of ‘Y’ by iterating over all positions from n-2 to 0.
  2. Now while iterating, if k<=n-i-1 then this is the required position of the leftmost occurrence of ‘Y’ and the position of rightmost occurrence is n-k so we can print the answer.
  3. Otherwise, let’s decrease k by n-i-1, i.e, remove all strings which have the leftmost ‘Y’ at the current position and proceed to the next position. In this way we consider all possible strings in lexicographic order.

Below is the implementation of the above approach. 
 




// C++ program find the Kth string in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the Kth string
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
void kth_string(int n, int k)
{
    // Iterate for all possible positions of
    // Left most Y
    for (int i = n - 2; i >= 0; i--) {
        // If i is the left most position of Y
        if (k <= (n - i - 1)) {
            // Put Y in their positions
            for (int j = 0; j < n; j++) {
                if (j == i or j == n - k)
                    cout << 'Y';
                else
                    cout << 'X';
            }
 
            break;
        }
        k -= (n - i - 1);
    }
}
 
// Driver code
int main()
{
    int n = 5, k = 7;
 
    // Function call
    kth_string(n, k);
 
    return 0;
}




// Java program find the Kth String in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
class GFG{
 
// Function to find the Kth String
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
static void kth_String(int n, int k)
{
     
    // Iterate for all possible
    // positions of eft most Y
    for(int i = n - 2; i >= 0; i--)
    {
       // If i is the left
       // most position of Y
       if (k <= (n - i - 1))
       {
           // Put Y in their positions
           for(int j = 0; j < n; j++)
           {
              if (j == i || j == n - k)
                  System.out.print('Y');
              else
                  System.out.print('X');
           }
 
            break;
        }
         
        k -= (n - i - 1);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5, k = 7;
 
    // Function call
    kth_String(n, k);
}
}
 
// This code is contributed by Amit Katiyar




# Python3 program find the Kth string in
# lexicographical order consisting
# of N-2 x’s and 2 y’s
 
# Function to find the Kth string
# in lexicographical order which
# consists of N-2 x’s and 2 y’s
def kth_string(n, k):
 
    # Iterate for all possible positions of
    # left most Y
    for i in range(n - 2, -1, -1):
         
        # If i is the left most position of Y
        if k <= (n - i - 1):
             
            # Put Y in their positions
            for j in range(n):
                if (j == i or j == n - k):
                    print('Y', end = "")
                else:
                    print('X', end = "")
            break
 
        k -= (n - i - 1)
 
# Driver code
n = 5
k = 7
 
# Function call
kth_string(n, k)
 
# This code is contributed by divyamohan123




// C# program find the Kth String in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
using System;
 
class GFG{
 
// Function to find the Kth String
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
static void kth_String(int n, int k)
{
     
    // Iterate for all possible
    // positions of eft most Y
    for(int i = n - 2; i >= 0; i--)
    {
         
       // If i is the left
       // most position of Y
       if (k <= (n - i - 1))
       {
            
           // Put Y in their positions
           for(int j = 0; j < n; j++)
           {
               if (j == i || j == n - k)
                   Console.Write('Y');
               else
                   Console.Write('X');
           }
           break;
       }
       k -= (n - i - 1);
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5, k = 7;
 
    // Function call
    kth_String(n, k);
}
}
 
// This code is contributed by Amit Katiyar




<script>
// javascript program find the Kth String in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
 
// Function to find the Kth String
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
function kth_String(n , k)
{
     
    // Iterate for all possible
    // positions of eft most Y
    for(i = n - 2; i >= 0; i--)
    {
       // If i is the left
       // most position of Y
       if (k <= (n - i - 1))
       {
           // Put Y in their positions
           for(j = 0; j < n; j++)
           {
              if (j == i || j == n - k)
                  document.write('Y');
              else
                  document.write('X');
           }
            break;
        }   
        k -= (n - i - 1);
    }
}
 
// Driver code
var n = 5, k = 7;
 
// Function call
kth_String(n, k);
 
// This code is contributed by Amit Katiyar
</script>

Output: 
YXXXY

 

Time Complexity: O(N2) // two loops are used nested inside each other so the complexity is n*n

Auxiliary Space: O (1) // no extra array is used so constant space 


Article Tags :