All Tracks Algorithms Dynamic Programming Problem

Digits and Fun
Tag(s):

Medium

Problem
Editorial
Analytics

Given a set of $$N$$ digits, find the $$k^{th}$$ smallest number that can be formed using them such that the sum of its digits is prime. If the answer does not exists or is greater than $$10^{18}$$ print $$-1$$.

Input:
First line consists of a single integer $$T$$ denoting the number of test cases.
Second line consists of a single integer denoting $$N$$.
Third line consists of $$N$$ space separated numbers denoting the set of digits. Fourth line consists of a single integer denoting $$k$$.

Output: Print the required answer in a new line for each test case.

Constraints:
$$1 \le N \le 9$$
$$1 \le k \le 10^{18}$$

SAMPLE INPUT
1
3
1 2 3
1
SAMPLE OUTPUT
2
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala 2.11.8, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

IIIT Jabalpur

Challenge Name

CodeRing - II

Notifications
View All Notifications