find the value at kth position when numbers are sorted lexicographically till n

-2

Ex :- Input: n = 12, k = 5

Output: ans = 2

Sorted list S: ["1", "10", "11", "12", "2", "3", "4", "5", ...., "9"] ans = 2

Vaibhav

Posted 2018-12-12T10:00:13.357

Reputation: 11

Question was closed 2018-12-12T10:47:46.820

Welcome to PPCG! This site is for recreational programming puzzles, not programming help. If you intend this as a proper challenge, you'll have to add a objective winning criteria (e.g. [tag:code-golf]) , remove the [tag:c++] tag and make it sound a lot less like "I want help with my homework" – Jo King – 2018-12-12T10:22:00.430

I think that this could be a good question for code-golf, provided that it isn't a duplicate. There are two remarks however, and both of them are that index-related. Since you say that you use C++ (which is 0-indexed), why are your numbers 1-12 rather than 0-11, and why is the answer a[4] if 5 is the input? Also, ╒░s\(§ and ╒░s§ are solutions in MathGolf for the original problem and a 0-indexed version. – maxb – 2018-12-12T13:10:52.960

Answers

1

Javascript (ES6), 55 bytes

(n,i)=>+[...Array(n).keys()].map(x=>x+1+"").sort()[i-1]

Try it online!

Unfortunately I can't comment yet, so I'll include it in my answer:

Welcome to PPCG! To make this a challenge you have to clarify some requirements of the challenge. Add a proper description, define the expected output and the winning criteria and also add some example tests.

zruF

Posted 2018-12-12T10:00:13.357

Reputation: 59

The default sort in javascript is lexicographic. So you can write .sort() instead of .sort((a,b)=>a.localeCompare(b)) – Spitemaster – 2018-12-12T13:29:23.123

@Spitemaster well, yea. Not sure why I didn't think of that. Thanks :) – zruF – 2018-12-12T13:37:39.187

0

R, 32 bytes

function(n,k)sort(c(1:n,"x"))[k]

Try it online!

J.Doe

Posted 2018-12-12T10:00:13.357

Reputation: 2 379