title: Kth element formed by joining N array sorted element in ascending order
body: <p>You are given a string S of length N. The string S consist of digits from 1-9. Consider the string indexing to be 1-based.
  You need to divide the string into blocks such that the i<sup>th</sup> block contains the elements from the index((i-1) * X + 1) to min(N,(i * X) (both inclusive). A number is <strong>valid</strong> if it is formed by choosing exactly one digit from each block and placing the digits in the order of their block number.</p>
  <p>For example :</p>
  <p>If the given string is '123456789' and X = 3, the blocks formed are [123],[456],[789]. Few valid number are 146,159,348 etc. but 124 and 396 are invalid.</p>
  <p>Among all the <strong>valid numbers</strong> that can be formed,I have to determine the <strong>K<sup>th</sup></strong> number if all the <strong>unique valid numbers</strong> are sorted in ascending order.</p>
  <p>My idea was to sort each block and then figure out which number will be the <strong>K<sup>th</sup></strong> number.
  But I got stuck on how to mathematically or any other approach for  find <strong>K<sup>th</sup></strong> element.</p>
  <pre><code>def getK(N:int, X:int, K:int, S:str):
      from math import ceil
      i = 1
      N = len(S)
      S = list(S)
      while i &lt;= ceil(N / X):
          start = (i - 1) * X
          end = min(N, i * X) - 1
          S[start:end+1] = sorted(S[start:end+1])
          print(S[start:end+1])
          i += 1
      print(S)
  </code></pre>
  <p>Please help me finding out way how to approach this problem.</p>