CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » March 2011 Challenge » K-Unique Sequence

K-Unique Sequence

Problem code: KUNIQUE

  • All Submissions

All submissions for this problem are available.

A sequence of N integers is called K-unique if, when it is split into N/K subsequences of K contiguous elements, each subsequence consists of K distinct integers.

For example, consider this sequence of 6 integers.
(2, 10, 2, 8, 3, 6)

This sequence is 2-unique, because when you split it into 3 subsequences each of 2 contiguous elements as below,
(2, 10), (2, 8), (3, 6),

each subsequence consists of 2 distinct integers.

You are given a sequence of N integers, and a positive integer K. Find a permutation of the sequence that is K-unique.

Input

The first line contains a single integer T, the number of test cases. T test cases follow. The first line of each test case contains two integers N and K. The next line contains a sequence of N integers ai, where ai is the i-th element of the sequence.

Output

For each test case, output a single line containing a permutation of the sequence that is K-unique. The elements of the sequence should be separated by a single space. If there are several solutions, output the lexicographically smallest one. If there is no solution, output -1.

Constraints

  • 1 <= T <= 5
  • 1 <= N <= 50000
  • 1<= K<= N
  • N will be divisible by K
  • 0 <= ai <= 1000000000

Example

Input:
3
1 1
42
4 2
4 7 7 7
6 2
2 10 2 8 3 6

Output:
42
-1
2 3 2 6 8 10

Author: fushar
Date Added: 25-10-2010
Time Limit: 5 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

What should be the output ...

gaj00 @ 1 Mar 2011 03:43 PM
What should be the output ... here

1 1
44

please clearify..

What makes it different from

ashar_adm @ 1 Mar 2011 03:55 PM

What makes it different from the first example test case?

Last test case is

devrajchouhan @ 1 Mar 2011 06:29 PM

Last test case is wrong....please admin check it ...

there is 2 8 2 10 3 6  are also valid solution,....

@Devraj: "If there are

ashar_adm @ 1 Mar 2011 06:41 PM

@Devraj: "If there are several solutions, output the lexicographically smallest one."

@all Singhs : Please read all

flying_ant @ 1 Mar 2011 06:56 PM

@all Singhs : Please read all the sections of the question carefully before asking a question here. Don't ask to check your code and read all the comments before posting your query.

For example input i have got

alanexe @ 1 Mar 2011 07:30 PM

For example input i have got good output but keep getting "wrong answer" after submit. What else sample input i can test for ? Any hints?

should two different sets

f1b0n4cc1 @ 1 Mar 2011 07:47 PM

should two different sets also be unique??

i.e.

for

6 3

2 3 4 2 3 4

is below soltn valid??

(2,3,4),(2,3,4)

1 <= N <= 50000 0 <= ai <=

Myth17 @ 1 Mar 2011 07:50 PM

1 <= N <= 50000

0 <= ai <= 1000000000

why is it 1000000000, shouldnt it be 50000 ? :|

Can anyone explain?

@Alan : We are not allowed to

flying_ant @ 1 Mar 2011 08:08 PM

@Alan : We are not allowed to share any thing more than what is given in the question, till the contest ends. Thinking of tricky cases is part of solving the problem.

@Unknown, Nitish : There are no such things mentioned. Do not assume anything. Problem statement is clear.

@Unknown Cipher: Yes, it is

ashar_adm @ 1 Mar 2011 08:42 PM

@Unknown Cipher: Yes, it is valid.

@Nitish: I don't see any problem with that constraint.

Problem Input statement

Myth17 @ 1 Mar 2011 09:12 PM

Problem Input statement says->

"The next line contains a sequence of N integers ai, where ai is the i-th element of the sequence."


For N the range mentioned is 1 <= N <= 50000

For the sequence 0 <= ai <= 1000000000

 

if its a sequence of N integer, it should be only 50000! Please correct me if I have misinterpreted some details.

@Nitish: The number of

ashar_adm @ 1 Mar 2011 09:16 PM

@Nitish: The number of elements can be up to 50000 and each element of it can be between 0 and 1000000000, inclusive. There's nothing wrong with this.

Thanks Ashar. It was a

Myth17 @ 1 Mar 2011 09:20 PM

Thanks Ashar. It was a misinterpretation from my side.

Something seems fishy... it

tijoforyou @ 1 Mar 2011 11:18 PM

Something seems fishy... it says, "lexicographically smallest one" and not "numerically smallest one"

So, shouldn't the answer to last test case have been 2 3 2 6 10 8, as "10" comes lexicographically before "8"?

(2,3),(2,6),(10,8)

in fact, 10 2 2 3 6 8 must

tijoforyou @ 1 Mar 2011 11:22 PM

in fact, 10 2 2 3 6 8 must come... then,the results must be numerically ordered... amn't i right?

is lexicographically here

krdeepak @ 2 Mar 2011 12:00 AM

is lexicographically here means just comparison between numbers. This is what i get from sample output.

@ All: Sequence A is

ashar_adm @ 2 Mar 2011 07:45 AM

@ All: Sequence A is lexicographically smaller than sequence B if A contains a smaller number at the first element they differ.

@Devraj Singh Chouhan: i

v_new.c @ 2 Mar 2011 02:25 PM

@Devraj Singh Chouhan:

i think u should read the question 3 or 4 times.

all ur doubts will become clear.

time exceed.....isn't the

rpbear @ 2 Mar 2011 03:08 PM

time exceed.....isn't the next_permutation of STL fast enough?

Are the subsequences

coders1122 @ 2 Mar 2011 03:19 PM

Are the subsequences non-overlapping?

i.e

For N = 4, K = 2

Can we say 1 2 1 1 is K-unique?

Because [1..2] is 2-unique and [2..3] is again 2-unique?

Assuming the last case is

coders1122 @ 2 Mar 2011 03:35 PM

Assuming the last case is correct.

I think my doubt is self answered.

The subsequences must indeed be "non-overlapping".

@ Ravi : If you split a

ashar_adm @ 2 Mar 2011 03:52 PM

@ Ravi : If you split a sequence of length N into N/K subsequences, each of K contiguous elements, they must be non-overlapping.

(1, 2, 1, 1) is not 2-unique.

My code runs perfect on every

Myth17 @ 2 Mar 2011 04:34 PM

My code runs perfect on every type of testcase I can conceive yet gives a wrong answer of codechef.

I have tried covering all corner cases on my own. Everything looks fine! :|

What shoud be an individual's strategy in such cases?

@Nitish : One thing would be

flying_ant @ 2 Mar 2011 04:48 PM

@Nitish : One thing would be generate lots and lots of test-data randomly with very low constraints on the input and check your ouput with that of a bruteforce code's. Though I hardly do that and usually take a long break away from computer :)

Its a deadlock condition for

Myth17 @ 2 Mar 2011 05:13 PM

Its a deadlock condition for me. :(

@Devraj Shing Chouhan : no

v_new.c @ 3 Mar 2011 03:23 AM

@Devraj Shing Chouhan : no need of answer ( specially when you also don't know ! )

/sources/Main.java:8: class

studd @ 3 Mar 2011 10:52 PM

/sources/Main.java:8: class TestUnique is public, should be declared in a file named TestUnique.java public class TestUnique{ ^ 1 error

what does this mean?

TestUnique is my class

studd @ 3 Mar 2011 10:53 PM

TestUnique is my class name

is it an error to declare it as public?

is the output of third input

studd @ 3 Mar 2011 11:01 PM

is the output of third input correct.....

shouldn't it be 2 6 2 8 3 10

can you provide test case

devrajchouhan @ 3 Mar 2011 11:03 PM

can you provide test case file of some input

Devraj singh chouhan...u are

nikunj165 @ 4 Mar 2011 02:48 AM

Devraj singh chouhan...u are a funny guy!!

@dharma : Your public class

ashar_adm @ 4 Mar 2011 06:48 AM

@dharma : Your public class name should be "Main".

(2 3 2 6 8 10) is lexicographically smaller than (2 6 2 8 3 10).

@Devraj : No.

In case of 2,10,2,8,4,2 is

jimmy valentine @ 4 Mar 2011 12:35 PM

In case of 2,10,2,8,4,2 is the split it into contiguous elements of the form (10,2) (8,4) (2,2) valid ? should first set of these subsequences (10,2) start necessarily from beginning ?

@myriad: The order of

ashar_adm @ 4 Mar 2011 01:53 PM

@myriad: The order of elements in the sequence must be preserved.

Thanks @ Ashar faudi

studd @ 4 Mar 2011 02:17 PM

Thanks @ Ashar faudi

my answer is showing run time

coding_geek @ 5 Mar 2011 07:35 PM

my answer is showing run time error everytime..why it is so ????

I too have the same error.

palcuiealex @ 5 Mar 2011 09:12 PM

I too have the same error.

@Ashar ... what you mean to

linuxfreak @ 6 Mar 2011 07:57 AM

@Ashar ... what you mean to say ... by order of elements in the sequence must be preserved.????  I can't see in the test cases the order has been preserved.... :-o

@Admin... I got wrong answer

linuxfreak @ 6 Mar 2011 08:24 AM

@Admin... I got wrong answer first... then I checked my codes logic.. and changed it... and it can't effect my running time.. :-o .. not it shows.. TL.... :-o .. how come??? wrong answer I can take... bt. TL exceed... on my system it runs for 0.23 sec... :-o

@Admin.. Oops... my

linuxfreak @ 6 Mar 2011 08:29 AM

@Admin.. Oops... my mistake... :-) ... I didn't tried for worst case... :-) .. sorry!!

@All... now am just blank..

linuxfreak @ 6 Mar 2011 09:15 AM

@All... now am just blank.. what to do... :-o ... after contest is over... I ll request to any one if interested and can tell me... why I am getting TLE.... :-( ... hoping for some one will help me out...

Thanks in advance... :-)

@Nitin: I meant, for example,

ashar_adm @ 6 Mar 2011 09:55 AM

@Nitin: I meant, for example, (1, 2, 3, 4) is split into (1, 2), (3, 4) not (1, 3), (2, 4). This was to answer myriad's clarification.

can we rotate lastand first

devrajchouhan @ 6 Mar 2011 12:51 PM

can we rotate lastand first element of this array.....?????

@Devraj: what do you mean by

ashar_adm @ 6 Mar 2011 01:49 PM

@Devraj: what do you mean by "rotate" ?

my means that..... 1 2 3 4

devrajchouhan @ 6 Mar 2011 03:37 PM

my means that..... 1 2 3 4 there are one continous seq. is (4,1) (2,3) after rotation of array...

The statement says nothing

ashar_adm @ 6 Mar 2011 03:58 PM

The statement says nothing about rotation.

huh!! lots of optimization!!!

codegambler @ 6 Mar 2011 10:03 PM

huh!! lots of optimization!!!

I am getting correct answer

dinesh_reddy @ 7 Mar 2011 12:15 PM

I am getting correct answer for all the test cases given in sum and comments but still getting wrong answer can any one suggest some critical test cases....

I tried the problem with

shishir_d_code @ 7 Mar 2011 10:40 PM

I tried the problem with three different logics. Each time the logic was more efficient in terms of time and space. Still the output is TIME LIMIT EXCEEDED. I know there is still room for optimization but now i am running short of ideas. Never mind. It's always a race between you and you :)

in following seq. 2 2 3 4 can

devrajchouhan @ 8 Mar 2011 09:55 AM

in following seq.

2 2 3 4

can we consider (4,2) as continous seq./////

@Devraj: No.

ashar_adm @ 8 Mar 2011 11:04 AM

@Devraj: No.

Hmm  writing out just the

yoghurt @ 8 Mar 2011 11:32 PM

Hmm  writing out just the 50000 numbers takes more time than 5 sec... what about that?:S

why my solution of O(nlgn)

hellomoto_35 @ 9 Mar 2011 12:43 PM

why my solution of O(nlgn) complexity is timing out?????

Of course because it takes

ashar_adm @ 9 Mar 2011 01:05 PM

Of course because it takes more than 5 seconds on some test files.

@Manoj:::in above case this

devrajchouhan @ 9 Mar 2011 05:39 PM

@Manoj:::in above case this is how this is continous seq. of 4 subseq.

(1,1,2,3) is not continous.

if i use list container and

hellomoto_35 @ 9 Mar 2011 11:03 PM

if i use list container and delete elements frm it...could it cause time limit exceeded????

@Nikhil - I am using the hash

chamanchindi @ 11 Mar 2011 10:48 AM

@Nikhil - I am using the hash set and still it is causing the time limit. So you can guess what the list will do.

@CodeChef Admin: When will we

pratikmoona @ 11 Mar 2011 12:30 PM

@CodeChef Admin: When will we be able to start submitting solutions again? For this contest... I get a "something wrong with the contest configuration" error whenever I submit.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com