id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

gaurav.gupta@mq.edu.au recursion > lexicographicPermutation
prev  |  next  |  chance

For integer n (assume n is between 1 and 9), we define lexicographic permutation as a permutation of digits 1, 2, ..., n such that permutation-k (k starting from 0) is greater than the permutation-(k-1).

As an example, for n = 4,

permutation-0 = "1234", permutation-1 = "1243", permutation-2 = "1324", ..., permutation-21 = "4231", permutation-22 = "4312", permutation-23 = "4321".

Define a recursive function that when passed the values of n and k, returns the permutation-k.

You may not use any loops.

Return an empty string if the value of n or k is invalid. Note that n needs to be between 1 and 9, and k will depend on the value of n. We will let you figure out the highest value of k in terms of n.


lexicographicPermutation(3, 3) → "231"
lexicographicPermutation(5, 10) → "13524"
lexicographicPermutation(8, 0) → "12345678"

...Save, Compile, Run (ctrl-enter)

String lexicographicPermutation(int n, int k) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 6 Post-solution available

Copyright Nick Parlante 2017 - privacy