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
    • February Long Contest
    • January CookOff
    • January Long Contest
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » Turbo Sort

Turbo Sort

Problem code: TSORT

  • Submit
  • All Submissions

All submissions for this problem are available.

Given the list of numbers, you are to sort them in non decreasing order.

Input

t – the number of numbers in list, then t lines follow [t <= 10^6].

Each line contains one integer: N [0 <= N <= 10^6]

Output

Output given numbers in non decreasing order.

Example

Input:

5
5
3
6
7
1

Output:

1
3
5
6
7

Date: 2008-12-01
Time limit: 5s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp HASK CAML CLPS PRLG WSPC BF ICK TEXT


  • Submit

Comments

  • Login or Register to post a comment.

ktulsyan @ 15 Jun 2009 06:08 PM

Somehow, this problem is timing out with JAVA using even the best time complexity u can have, which is O(n)....

So far, only 1 person has been able to solve this using JAVA...what I mean is are JAVA programmers inferior to their C counterparts, or is it something with the constaints ??

rooparam @ 17 Jun 2009 12:52 AM

it even timed out when using cout : cin in C . so i have to implement counting sort using scanf:printf

darthsitius @ 23 Jun 2009 08:32 PM

Inspite of using a good compexity of quicksort ! It times out!!

kam @ 24 Jun 2009 07:47 AM

i have tried so much times but every time the problem compiled and run on my system but on codechef it gives compilation error plz help i m frusted now

i0exception @ 24 Jun 2009 05:11 PM

@Kshitij I believe the time limit for java is 2x the time limit for C programs.

i0exception @ 24 Jun 2009 05:13 PM

@varrun Quicksort has a worst case complexity of O(n^2)
@kamlendra codechef uses the GNU compiler collection. What compiler are you compiling on?

kevmo314 @ 1 Jul 2009 10:07 AM

Merge sort didn't work for me either. Even heap sort so it can sort while reading input didn't work in Java...

sid_chilling @ 6 Jul 2009 05:36 AM

using the in-built Arrays.sort() times out in Java... so what is the solution to this problem?

i0exception @ 6 Jul 2009 05:17 PM

@siddharth Write your own sort function?

delta @ 14 Jul 2009 10:37 PM

The top 6 contenders here (the best competitors on codechef...) have somehow managed to optimize their code to an extent that is much greater than the pack (which includes me here). Everyone, except these 6 are beyond 2 seconds. Anybody knows how to get down to those time scales?

bchoii @ 29 Jul 2009 12:30 AM

What other optimizations are possible in Java ? The algorithm is already O(n). Using primitive arrays... any pointers would be appreciated.

admin 2 @ 29 Jul 2009 12:34 AM

Faster IO maybe?

mkeeler @ 29 Jul 2009 01:05 AM

I have implemented my solution in c, and it runs fine on my machine. However on their servers it says I get the wrong answer. Is there any way to figure out where they were saying mine went wrong.

admin 2 @ 29 Jul 2009 01:08 AM

Not at the moment. You need to take care that the numbers need not be distinct.

prm2612 @ 29 Jul 2009 12:05 PM

@Directi Admin
Thanks for the big "faster IO" hint!!! :)

rohanbk @ 8 Aug 2009 08:04 PM

I implemented Merge-sort in Java and I ran out of time :(

blablub @ 13 Aug 2009 06:35 AM

I programmed the solution in C. For 10^6 input values my program runs 1.2sec on my machine.
Then I ported the code to java. It uses primitive arrays, data and operations all over. I read input using System.in.read() - the result is a run time of 17sec.
I'm so curious if someone has solved this using java and how!

i0exception @ 13 Aug 2009 06:43 PM

This problem has been solved in Java. See the successful submissions' list.

blablub @ 14 Aug 2009 01:14 AM

Hi, I've contacted that person and am waiting for an answer. My suggestion is that the i/o part is the bottleneck. What means of i/o should one use in Java generally? Thx.

leppyr64 @ 14 Aug 2009 05:06 AM

For Java, it's suggested to create your own reader class.
http://www.spoj.pl/forum/viewtopic.php?f=43

nighthunter33 @ 14 Aug 2009 06:03 PM

I got a timeout error for this, does that mean that the solution was right it just took too long, or does the program cut off at 5s and it could still be a wrong answer?

admin 2 @ 14 Aug 2009 08:09 PM

A timeout means that your code took more than the specified time. It says nothing about the correctness of your code.

guys since we know the range

rajarshi99 @ 21 Aug 2009 10:48 AM
guys since we know the range of the numbers we can use Counting Sort which will be O(n) .Comparison sort based algos wont work with this problem . Besided ,using fread etc for I/O will further speed up the soln .the secret to a successful soln lies in the algo .

This can be solved using

admin @ 21 Aug 2009 01:18 PM

This can be solved using comparison based sorting techniques :)

yes ,using comparison based

rajarshi99 @ 21 Aug 2009 08:25 PM
yes ,using comparison based techniques this can certainly be solved, but keeping within the time limit of 5s may become very difficult. counting sort works much faster.

why i am getting a wrong

anupam @ 9 Sep 2009 01:38 PM

why i am getting a wrong answer? i did merge sort. when i am testing with numbers i am getting right answers. Admins kindly please check.

@admin: Can you please tell

gaurav_gopi123 @ 16 Sep 2009 09:41 AM

@admin: Can you please tell more about faster I/O? Are you talking about NIO Apis or are you suggesting to use the Buffered I/O?

Any kind of I/O which is

admin @ 16 Sep 2009 01:24 PM
Any kind of I/O which is faster than the normal methods of printf(), scanf() etc are considered to be faster I/O. You can do this in a lot of different ways.

which sort method can be used

swaminathan @ 18 Sep 2009 08:55 AM
which sort method can be used in this problem

There are a lot of different

admin @ 18 Sep 2009 02:21 PM
There are a lot of different sorting algorithms that can be used to sort the numbers here. Most efficient implementations of algorithms with complexity O(n logn) should clear.

mine works well with C++ but

raj_aryan @ 23 Sep 2009 02:31 PM

mine works well with C++ but dosnt work with java. Same code gives compile time error in JAVA i dont know why.

Please read the FAQ

admin @ 23 Sep 2009 02:33 PM

Please read the FAQ

The example for Turbo Sort is

bermanrl @ 26 Sep 2009 12:59 AM

The example for Turbo Sort is in error. According to the instructions you want sorted output in non decreasing order. Your output is 1 3 5 6 7 which is an example of increasing order. Non decreasing order would be 1 3 5 5 6 7. Do you want decreasing order as specified or increasing order as shown?

Thank you,

 

Robert

 

 

@robert non decreasing order

ravi_m @ 26 Sep 2009 01:37 AM
@robert non decreasing order means !(decreasing order) which IS the "increasing" order

@robert the first number in

shivmitra @ 26 Sep 2009 02:24 AM
@robert the first number in the input in the no. of inputs tht r to b sorted ...so first 5 in the input says tht there r 5 numbers tht r to b sorted.....jst read the ques carefully....

Ravi, Shivmitra, Thank you

bermanrl @ 26 Sep 2009 02:30 AM

Ravi, Shivmitra,

Thank you very much.

Robert

Even by using treeset its

sai anusha @ 1 Oct 2009 12:48 AM

Even by using treeset its giving timeout............wat is the soln for this problem in Java

Very odd discrepencies

ben_88 @ 1 Oct 2009 12:33 PM

Very odd discrepencies between my results on my machine and on the server machine...

My first method:
My machine: 2.5 secs
Server: 3.14 secs

Second highly optimized method:
My machine: 0.405 secs !!!!
Server: 3.78 secs ?!

This is very frustrating, are there any technical specs for the server / build command? I'd like to know how such a huge improvement on my end actually slowed down my running time!

If you calculate the performance difference between the machines:
3.14 / 2.5 = 1.256
Based on that number, the approximate runtime would be expected to be roughly:
0.405 * 1.256 = 0.509 secs !! (Faster than the current best!)

With such discrepancies, it's extremely difficult to predict how any changes will affect the server's runtime numbers.. so I'll just give up on this and take it as a personal win!! ;)

If only michael hayter could send me his code so I can compare on my machine >:-)

why is it coming runtime

satyajeet parida @ 1 Oct 2009 11:42 PM
why is it coming runtime error ??? its working fine on bloodshed dev ....

Please read the FAQ and

admin @ 2 Oct 2009 11:47 AM

Please read the FAQ and Sample Solutions

I have had seven TLE's on

bermanrl @ 19 Oct 2009 06:21 PM

I have had seven TLE's on this sort. My latest algorithm has been a very simple counting sort. It is designed to work with a maximim number of 1,000,000 numbers between 0 and 1,000,000. To test the algorithm I used a random number generator to generate a file of first 100 numbers to check the accuracy of the sort and then 1,000,000 numbers to check timing.  The one hundred number test ran well as did the million number test. The language used is Python. The timings for the million item test run was a user time of 4.588 seconds and a system time of 1.640 seconds. Total of 6.228 seconds. In reviewing the results for those python programs which passed this test, the results ran from 5.98 seconds to 14.59 seconds. Doubling my time to possibly account for timing differences between your machine and mine that still places me in the passing range.

Is it possible for one of your moderators to look at my code and tell me if:

1)  I am approaching the problem from the totally wrong perspective,

2)  The approach is correct but that some of the code is superfluous, and,

3)  If the approach is correct, and I have a silly glaring error in logic, perhaps a hint would be truly appreciated.

I will be happy to send you the code if you cannot review the latest run.

 

Thank you,

 

Robert

 

why doesn't non-decreading

TrialAndError @ 19 Oct 2009 10:02 PM

why doesn't non-decreading order mean anything but non-decreasing order?

 

It could be scrambled numbers and still be non decreasing order. which means that it should not necessiarly be in increasing order.

 

You should clarify the problem.

@Robert You might want to try

admin @ 19 Oct 2009 11:20 PM

@Robert You might want to try an NlogN algorithm.

 

@Notneeded Could you give an example of what you mean?

I keep getting a segfault on

jcomeau_ictx @ 22 Oct 2009 07:20 AM

I keep getting a segfault on your machine with my nasm program, which runs fine on my laptop. I've tried it with various boundary conditions: a file with just one line of "0"; a file with "1" followed by "0"; your sample inputs above; and with 1000000 inputs ranging from 0 to 1000000. I've made sure it works with both unix (n) and windows (rn) newlines. Any hints as to boundary conditions I haven't considered?

@notNeeded: it says

jcomeau_ictx @ 23 Oct 2009 02:52 AM

@notNeeded: it says "non-decreasing" instead of "increasing" because there could be duplicate samples, in which case it won't increase at all from one to the next; non-decreasing in this case means it NEVER decreases from one output to the next. Scrambled numbers thus wouldn't qualify.

10
10
11
22 } non-decreasing
22
33

10
11
10 <- decrease from 11 here
22
33
22 <- decrease from 33 here

what's wrong with this

bezgin @ 23 Oct 2009 12:13 PM

what's wrong with this approach?

 

#define RANGE 1000001
#include <iostream>
int main() {
int T,num; scanf("%d", &T);
unsigned int arr[RANGE];
for(int i=0; i<RANGE; i++) arr[i] = 0;
while(T--) {
scanf("%d", &num);
arr[num]++;
}
for(int i=0; i<RANGE; i++) {
while(arr[i]--) printf("%dn",i);
}
return 0;
}

You don't initialise the

triplem @ 23 Oct 2009 01:03 PM

You don't initialise the values of arr, which is a local variable thus won't be 0s.

that bug bit me once too, in

jcomeau_ictx @ 24 Oct 2009 12:13 AM

that bug bit me once too, in another program. if you place your arrays outside of a function, they'll be placed in the .bss segment, which IIRC is guaranteed by the ELF loader to be zeroed.

@bezgin: that's the approach

jcomeau_ictx @ 24 Oct 2009 12:15 AM

@bezgin: that's the approach used by my C program, but I had to replace scanf and printf with optimized routines in order to get the execution time down.

finally got my nasm program

jcomeau_ictx @ 24 Oct 2009 12:21 AM

finally got my nasm program running on CodeChef! the "boundary" condition I wasn't prepared for was lack of a newline at the end of the last sample!

lol at your nasm adventure

bezgin @ 24 Oct 2009 07:49 AM

lol at your nasm adventure :)

my turbosort works fine now, i had OFF-BY-ONE bug, the above code works! Thx.

@bezgin: you're right, the

jcomeau_ictx @ 24 Oct 2009 09:00 AM

@bezgin: you're right, the merry man was wrong, you DID initialize the array to zeros. but if you'd put the array outside of main(), you wouldn't have had to.

my optimized assembly language is only 5 hundredths of a second faster than my C program! and far less maintainable! wtf!?!?! think it's time for a beer...

No he didn't, and no I wasn't

triplem @ 24 Oct 2009 03:26 PM

No he didn't, and no I wasn't :P He edited his comment to have accepted code as his last post states ;)

@Stephen: ah. my bad. wasn't

jcomeau_ictx @ 24 Oct 2009 04:56 PM

@Stephen: ah. my bad. wasn't aware that comments could be edited.

Is it right to try and use

JoeVaulter @ 29 Oct 2009 03:17 AM

Is it right to try and use threads in java for this program?

I also got a runtime error, would that be because i used up to much memory?

Ok i cant figure out the

JoeVaulter @ 29 Oct 2009 09:50 AM

Ok i cant figure out the answer. Im reading everything into an int array[] doing Arrays.sort() then printing everything out. I have been messing around with output but cant seem to find anything. Can any one tell me where i could atleast start looking for an answer?

Try moving w.flush() outside

triplem @ 29 Oct 2009 10:42 AM

Try moving w.flush() outside of the loop.

i used w.flush() but little

JoeVaulter @ 29 Oct 2009 11:33 PM

i used w.flush() but little boxes are printed out instead of integers

You have got to be kidding

JoeVaulter @ 30 Oct 2009 10:01 PM

You have got to be kidding me.... i figured it out. Wow... this was bugging me.

wrong answer plz

harinderpal5 @ 5 Nov 2009 01:19 AM

wrong answer plz help

 

#include<stdio.h>
int main()
{
int i,j,temp,a[100],t;
if(t<=1000000)
{
scanf("%d",&t);
}
else
{
return 0;
}
for(i=0;i<t;i++)
{
scanf("%d", &a[i]);
if(a[i]>1000000)
{
return 0;
}
}
for(i=0;i<t;i++)
{
for(j=i;j<t;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<t;i++)
{
printf("%dn",a[i]);
}
return 0;
}

There are two lines in the

triplem @ 5 Nov 2009 02:09 AM

There are two lines in the section called 'Input'. Try reading the first one.

the input conditions are met

harinderpal5 @ 5 Nov 2009 02:16 AM

the input conditions are met in my program .... i m not able to understand the error!! plz help

Tell me what the first line

triplem @ 5 Nov 2009 03:03 AM

Tell me what the first line of the input says. And then how that is met by your program.

I tried count sort with

depy @ 5 Nov 2009 08:13 AM

I tried count sort with python. Still get TLE.

Any suggestions?

 

I used xrange instead of range. And I initialized the array using:

numbers = [0] * 1000001

 

Is that okay?

I have implemented counting

shakir @ 10 Nov 2009 08:53 PM

I have implemented counting sort in ruby. Still it gives me time limit exceeded. What should I do ?

how to define this big array

shanky89 @ 19 Nov 2009 03:20 PM

how to define this big array in C .  My programme in codeblocks doesn't give any compile time error  or warning , but on running it computer is hanging .

 

 

On reducing the array length programme in working fine.

 

help

Try declaring it outside of

triplem @ 19 Nov 2009 03:37 PM

Try declaring it outside of any function.

what is better I/O than

gunjanbansal @ 23 Nov 2009 12:52 PM

what is better I/O than printf and scnaf ?? i tried to figure out but cudnt

does codechef passes files

gunjanbansal @ 23 Nov 2009 01:07 PM

does codechef passes files as

< filename to the executables ???

I believe it would be very

jcomeau_ictx @ 24 Nov 2009 10:52 AM

I believe it would be very difficult to get an interpreted language like Python or Ruby to pass this test. Even using psyco for JIT compilation rarely gets you even close to C or C++ speeds. Python is good for many problems but not this particular one. Probably same story with Ruby, I'm not as familiar with it.

I usually prototype a solution in Python, then port to C for speed once the algorithm is somewhat optimized. Don't know if it's the best approach but it works for me. When it doesn't it usually means my algorithm sucks.

I just can't pass this "Time

maximbu @ 25 Nov 2009 04:41 AM

I just can't pass this "Time Limit Exceeded" error . Did anyone managed to solve this using C# ?

my third try gave me correct

keedap @ 25 Nov 2009 01:26 PM

my third try gave me correct answer ..

...

I am using count sort in C . and getting time of 2.33 sec

hmmm im having problems

Anirudh_s @ 30 Nov 2009 05:01 PM

hmmm im having problems declaring an array with 10^6 size ....help guys??

First, i'm using java, i did

coalwater @ 6 Dec 2009 04:51 AM

First, i'm using java, i did the o(n) sort thing, then i found that i got the TLE error, so i tried using faster input and output according to the admin's hint and what this page said but i still couldn't work it out, i still get the TLE error.. what else could i do.. cause i still see java programmers who found a way to solve this.. any hints?

Your read method isn't very

triplem @ 6 Dec 2009 04:59 AM

Your read method isn't very good at all; it is probably slower than just using a BufferedReader. You are using immutable strings and concatenating these multiple times which is very very slow. You should be writing your own parseInt function which works directly with the characters, not using Strings at all.

ok finally got it to work

coalwater @ 6 Dec 2009 08:48 AM

ok finally got it to work lol, had to look around for fast input AND output codes :(

hello everyone, if anybody

shailendra_ameta @ 11 Dec 2009 02:42 PM

hello everyone,

if anybody submitted this problem in pythonthen pls help me ....

You've gotta know some asm

Dudio @ 27 Dec 2009 11:02 PM

You've gotta know some asm for this

Wow...I don't understand why

Dudio @ 27 Dec 2009 11:39 PM

Wow...I don't understand why people don't make their questions clearer....

 

Instead of trying to be all "l33t", just write out the question exactly how you want it solved.

How could this question

triplem @ 28 Dec 2009 02:03 AM

How could this question possibly be made clearer?

@Shailendra: my Python

jcomeau_ictx @ 28 Dec 2009 03:03 AM

@Shailendra: my Python solution took over 12 seconds to run, but here's the code: http://www.codechef.com/viewsolution/113103

sir my program is here.there

rajesh bhatia @ 1 Jan 2010 05:58 PM

sir my program is here.there is runtime error.

cn u tell me wht problem is der?

my programm:

#include<stdio.h>
int main()
{
  long int *p;
  long int no;
  long int i,j,temp;

  printf("input:n");
  scanf("%ld",&no);
  for(i=0;i<no;i++)
  {
     scanf("%ld",(p+i));

  }
  for(i=0;i<no;i++)
  {
     for(j=i+1;j<no;j++)
     {
     if(*(p+i)>*(p+j))
 {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
 }
     }
  }
  printf("noutput:n");
  for(i=0;i<no;i++)
  {
    printf("%ldn",*(p+i));
  }
  return 0;
}

tell me wht's the run time error.

my id_:rajesh91bhatia@gmail.com

 

hello sir how can i find the

rajesh bhatia @ 1 Jan 2010 05:59 PM

hello sir

how can i find the compiletion time??????????///

pls tell me

 

What do you mean by that?

admin @ 1 Jan 2010 06:19 PM

What do you mean by that?

Hi, I'm using a counting sort

saurajit11 @ 11 Jan 2010 06:31 PM

Hi,

I'm using a counting sort in C and I'm using the int_fast32_t data type. However I'm still getting TLE! I'm attaching my code. Can you tell me why this isn't working within the time limits. Thank you

#define MAX 1000001
#include<stdio.h>
#include<stdint.h>
#include<inttypes.h>

int_fast32_t arr[MAX];
int_fast32_t i,num;

int main()
{
int_fast32_t T;
scanf(SCNdFAST32,&T);

for(i=0; i<t; i++)
{
scanf(SCNdFAST32,&num);
arr[num]++;
}
for(i=0; i<MAX; i++)
{
while(arr[i]--)
printf(PRIdFAST32"n",i);
}   
}

Ok, so I went back and tried

saurajit11 @ 14 Jan 2010 10:47 PM

Ok, so I went back and tried doing this program in Java. I'm using a counting sort and my own input routine which reads in 64K bytes at one go into a buffer. The code is given here:

http://www.codechef.com/viewsolution/168217

I understand that C will be much faster; but I would really like to know what part of my program is causing Time Limit Exceeded given that it's in Java which is a language I'm allowed to code in.

Please don't reply Java is too slow etc etc. I would really appreciate it if someone could point me to how to make the exisiting Java code work within the time constraints. Thank you.

CAN ANYONE TELL ME OR PROVIDE

hiren.sharma @ 19 Jan 2010 01:22 PM

CAN ANYONE TELL ME OR PROVIDE ME WITH A LINK ON HOW TO CREATE MY OWN IO CLASSES IN C,C++ ????

plzzzzzzzzz HELP !!!!!

@Hiren: there are plenty of

jcomeau_ictx @ 19 Jan 2010 01:40 PM

@Hiren: there are plenty of world-readable solutions here at CodeChef.com. Look at the fastest programs in C and C++ and you will find an assortment of highly optimized input routines. You don't really mean "classes" in the OO sense, do you?

@Saurajit: some suggestions:

jcomeau_ictx @ 19 Jan 2010 01:48 PM

@Saurajit: some suggestions: have you created your own stress testdeck? Have you tried one of the profilers available for Java? Have you commented out parts of the code to see if you can get WA rather than TLE?

IT RUNS FINE ON MY COMPUTER

hiren.sharma @ 20 Jan 2010 11:26 AM

IT RUNS FINE ON MY COMPUTER WATS WRONG WITH IT. IT GIVES RUNTIME ERROR ON OUR SITE

THE OUTPUT IS LIKE THIS:


5
5
3
6
7
1

1
3
5
6
7

plzzzzzzzzz tell me wats wrong, why it gives runtime error. m using gcc compiler

IS IT BECAUSE OF THE LINE

hiren.sharma @ 20 Jan 2010 11:32 AM

IS IT BECAUSE OF THE LINE SPACE BETWEEN INPUT AND OUTPUT

 

You have already solved your

triplem @ 20 Jan 2010 03:23 PM

You have already solved your own problem, but please read the FAQ before posting a comment.

@java developers.   tis

schampiri @ 20 Jan 2010 05:06 PM

@java developers.

 

tis possible to use Arrays.sort(). and get the job done. (I did it) u just need faster I/O

 

System.out.println() for output wont do it for u neither will using a BufferedReader for input

@anthony and john   Thanks

saurajit11 @ 23 Jan 2010 04:51 PM

@anthony and john

 

Thanks for the input! Unfortunately, I'm new to this and I don't know how to create a stress test deck for my java program.

I reworked the output part of the code. Earlier I was using System.out.println. Now, this is the section that outputs (i'm using counting sort and the buffer length is 8192):

oplen=0;     
for(i=0; i<1000001; i++)
{    while(arr[i]-- >0 )
{
String s=Integer.toString(i);
int len = s.length();
char[] temp = s.toCharArray();
if(oplen<SIZE-len-1)
{
for(int jj=0; jj<len; jj++)
buf[oplen++]=(byte)temp[jj];
buf[oplen++]=10;                         //newline
}
else
{
System.out.write(buf,0,oplen);
oplen=0;
}
}
}
//System.out.println(Arrays.toString(buf));   
if(oplen>0)
{   
System.out.write(buf,0,oplen);        
}

But now I'm getting a WA!! Since this is the only part of the code I've changed, I'm assuming the error's here, but for the life of me I can't figure out what it is!

we can't use vectors ?

sushil @ 24 Feb 2010 09:18 PM

we can't use vectors ?

Sure you can. Except you've

triplem @ 25 Feb 2010 02:53 AM

Sure you can. Except you've submitted your code as C code; they don't exist in C.

please show the solutions as

disciple1 @ 25 Feb 2010 05:36 PM

please show the solutions as well plzzzzzz

counting sort get accepted in

codegambler @ 14 Mar 2010 06:31 PM

counting sort get accepted in 2.** sec, And further doing fast INPUT OUTPUT method decrease to 0.9 sec :)

Hi, I just submitted my bash

sharfah @ 1 Apr 2010 08:00 PM

Hi,

I just submitted my bash script but it failed with a runtime exception. Is "sort" installed?
Is there a list of all the unix utilities installed on your system (e.g. sort, cut, grep, bc etc)?

Thanks

Hi I was wondering what else

ace33 @ 14 May 2010 08:43 AM

Hi I was wondering what else I can do to speed up my code. I have changed all cin and cout to printf and scanf, and have eliminated all loops that seemed excessive, and have only one while loop. Does anyone have any other suggestions as to how to speed up my code.

@bob i think there is some

devendra088 @ 14 May 2010 06:57 PM

@bob

i think there is some problem in code inside while loop ... its not working properly...

try algorithm of O(nlog(n)) like quick sort to solve this problem in time limit ... for better u can also use liner complexity algo but i think quick sort is sufficient to solve in time limit

@Devendra Pratap

ace33 @ 15 May 2010 12:04 AM

@Devendra Pratap Singh

 

Thanks both of your suggestions led to solutions under the time limit.

ok I implemented MergeSort

numbnut @ 18 May 2010 01:13 AM

ok I implemented MergeSort and its working fine on my PC.

But when i load it up on the server, It says wrong answer. Not Time Out, Wrong Answer.. how can that be? its working fine on the PC!!!!

runtime error plz

mimo91 @ 20 May 2010 12:25 PM

runtime error plz help

#include <iostream>

 

using namespace std;

 

int main()

{

int n[100],k,t;

cin>>t;

for(int i=0;i<t;i++)

{cin>>n[i];

for(int j=0;j<i;j++)

{if(n[j]<n[i])

{continue;}

else {

k=n[i];

n[i]=n[j];

n[j]=k;}

}

}

cout<<"the outputn";

for(int m=0;m<t;m++)

cout<<n[m]<<endl;

return 0;

}

Read the input/output format.

triplem @ 20 May 2010 12:36 PM

Read the input/output format. The errors are obvious.

hi :) just a quick question

arya5691 @ 21 May 2010 05:23 PM

hi :)

just a quick question to those who successfully submitted solutions to the above question in PYTHON... which algorithm would be suitable??

I used bucket sort, counting sort, as well as running an insertion sort over the elements after dividing them into buckets... but I'm still not able to get the code to run within the specified time limits. Can someone please give me some suggestions ?? Any help would be appreciated :) thanks

how do u input and store such

kkneeraj @ 23 May 2010 02:19 PM

how do u input and store such large set of numbers ?

please tell me ????

@Admin : Please Help me out

ChunChun @ 27 May 2010 12:24 AM

@Admin :

Please Help me out with this problem, can't seem to find what is going wrong. It gives TLE, i have tried all the methods i know. Please help.

The links to two different approaches are:

http://www.codechef.com/viewsolution/245663

http://www.codechef.com/viewsolution/245683

both give wrong answers :(

 

PLEASE HELP :(

@Amit : There is nothing you

codegambler @ 27 May 2010 10:07 AM

@Amit :

There is nothing you need to do in it. just take input sort using STL function sort. that will accepted.

@Pankaj Sir, i would still

ChunChun @ 27 May 2010 05:17 PM

@Pankaj

Sir, i would still want to know the problem with my approach. I mean are all the type of input methods i'm using giving these TLE errors ? Because i can't seem to find why the randomized QS with expect n*logn time shouldn't work.

The first thing I tested your

triplem @ 28 May 2010 06:05 AM

The first thing I tested your code on was a million ones. It still hasn't finished running.

Hey Stephen, thanks for your

ChunChun @ 28 May 2010 06:10 PM

Hey Stephen, thanks for your valuable hint :)

runtime error...plz

scorpio90 @ 9 Jun 2010 10:09 PM

runtime error...plz help

 

#include<iostream>

#include<stdlib.h>

//#include <conio.h>

 

using namespace std;

 

void swap(int *x,int *y)

{

int temp;

temp=*x;

*x=*y;

*y=temp;

}

int partition(int a[],int p,int r)

{

int x=a[r];

int i=p-1;

for(int j=p;j<=r-1;j++){

if(a[j]<=x){

i=i+1;

swap(&a[i],&a[j]);

}

}

swap(&a[i+1],&a[r]);

return i+1;

}

void quicksort(int a[],int p,int r)

{

if(p<r){

int q=partition(a,p,r);

quicksort(a,p,q-1);

quicksort(a,q+1,r);

}

 

}

 

int main()

{

int n;

int a[100]={1};

//cout<<"enter the number of elements"<<endl;

cin>>n;

//cout<<"now enter the elements"<<endl;

for(int i=0;i<n;i++)

cin>>a[i];

quicksort(a,0,n-1);

//cout<<"sorted array is---->"<<endl;

//cout<<a[0];

for(int i=0;i<n;i++)

cout<<a[i]<<endl;

//getch();

system("pause");

return 0;

}


read input statement

devendra088 @ 9 Jun 2010 10:34 PM

read input statement again

 

t – the number of numbers in list, then t lines follow [t <= 10^6].

 

array u use can store maximum 100 number  while input can be till 10^6  ... thats why it result to run time error

my program run correct on my

bhogal08547 @ 9 Jun 2010 11:01 PM

my program run correct on my system. But gave run time error on code craft the code is

#include<iostream>
using namespace std;
int t,a[100];
void del(int);
int main()
{
int i,j,k;
cin>>t;
for(j=0;j<t;j++)
cin>>a[j];  
for(j=0;j<t;j++)
for(k=j+1;k<t;k++)
{ if(a[j]>a[k])
{int temp;
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
a:
for(k=0;k<t;k++)
{  if(a[k]==a[k+1])
{ for(int i=k;i<t;i++)
a[i]=a[i+1];
t--;
goto a; 
}                        
}
for(j=0;j<t;j++)
cout<<a[j]<<"n";
return 0; 
}

 

please any body help if their is problem in input or output..........

i will highly thankful to him .My email id-bhogal.1198@gmail.com

@above   reason for runtime

devendra088 @ 9 Jun 2010 11:23 PM

@above

 

reason for runtime eroor is that u r using array of size 100 ... so how it can store more than 100 element...

change ur array to a[1000010]

<pre>a   a</pre>

digvijay1248 @ 13 Jul 2010 09:21 PM

<pre>a   a</pre>

Can anyone please tell me

digvijay1248 @ 13 Jul 2010 09:27 PM

Can anyone please tell me what improvements can be done to this code in JAVA to pass the time limit. Its giving time limite exceeded (TLE). I have already tried scanning all the input and then doing the computation, that didnt work either.

counting sort

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;

/*counting sort*/

public class dexp {
    public static void main (String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out, true);
        int t = Integer.parseInt(in.readLine());       
        int[] frequency = new int[t];
        for(int i = 0; i < t; i++)
            frequency[Integer.parseInt(in.readLine())]++;
        for(int i = 0; i < t; i++) {
            for(int j = 0; j < frequency[i]; j++)
                out.println(i);
        }
    }
}

class name that I submitted

digvijay1248 @ 13 Jul 2010 09:29 PM

class name that I submitted was Main.java so thats not the error.

@digviay Singh /

schampiri @ 14 Jul 2010 02:56 PM

@digviay Singh / saurajit

instead of using a PrintWriter try using

PrintStream out =new PrintStream(new BufferedOutputStream(System.out));

@anthony musyoki Thanks, it

digvijay1248 @ 15 Jul 2010 07:31 AM

@anthony musyoki

Thanks, it worked, though it showed 7.63 seconds. I need to work on input method.

how can i see others'

jatingandhi28 @ 16 Jul 2010 09:37 PM

how can i see others' solution of this problem????

can any one sort this array

jatingandhi28 @ 16 Jul 2010 09:40 PM

can any one sort this array with complexity O(n)?????

an array normally doesnt take

divyark @ 27 Jul 2010 03:48 PM

an array normally doesnt take more than 10^4...... it gives a compile time error if we try to declare more than this.... how can i declare an array of max size 10^6.....

I get this error when I

geekru2 @ 28 Aug 2010 07:21 PM

I get this error when I submit! contest_problem_id field is required.

Any headers I need to place in the file?

Each line contains one

jravi96 @ 4 Sep 2010 04:37 PM

Each line contains one integer: N [0 <= N <= 10^6]

 

the above line means the value of N should not exceed 10  ,am i correct?

plz let me know.

@ravi: No , that

firassalloum @ 6 Sep 2010 04:13 AM

@ravi:

No , that means

N<=1000000

I first tried implementing

abhishek_t @ 10 Oct 2010 10:22 PM

I first tried implementing quick sort in C++, but got runtime error. I guess I was using up too much memory to create vector<unsigned int>s. Then I used std::sort and it worked just fine.

i used bubble sort can anyone

sidcsevssut @ 18 Oct 2010 10:30 AM

i used bubble sort can anyone tell the mistake in my code

 

 

 

Please don't post your code

triplem @ 18 Oct 2010 11:31 AM

Please don't post your code here. Have you tested your code on an input of size 1000000? Bubble sort is O(n^2), so it will be far too slow.

Simple printing of numbers

avinash1 @ 19 Oct 2010 12:43 PM

Simple printing of numbers from 1 to 1000000 takes lot of time in C#

int start = Environment.TickCount;
for (int i = 0; i < 1000001; i++)
Console.WriteLine(i);
int stop = Environment.TickCount;
int time = stop - start;

I got time = 123210 which is in ms. so it comes out ~= 2 minutes. I have latest PC configuration again.

Are you sure you are timing

triplem @ 19 Oct 2010 02:57 PM

Are you sure you are timing the execution time of your program, and not how long it takes to display the numbers on your screen? (You should test by redirecting output to a file as mentioned in the FAQ).

then wht is the solution for

lukka @ 19 Oct 2010 07:02 PM

then wht is the solution for storing 10^6 nos. if array can only store 100. why does not it shows error while in my computer ?

An array can easily store

triplem @ 20 Oct 2010 01:54 AM

An array can easily store 10^6 numbers..

Hi Stephen, I didn't get you

avinash1 @ 20 Oct 2010 08:42 AM

Hi Stephen,

I didn't get you exactly & from FAQ, So do we need to direct the output to console or to out.txt? directly to console takes time in minutes while directing to txt file doesn't take time I mean hardly 1-2 sec. FAQ & test programs given tells it should be directed to console output except for testing which tells out.txt. And after submission it shows time limit exceeded if directed to console output & runtime error if directed to "out.txt"?

Please help on this one.

Your program should always

triplem @ 20 Oct 2010 09:16 AM

Your program should always write to the standard output stream. But when testing your program, Codechef will redirect the standard output stream to a file, it will not run your program and display the results on its 'screen' (which is the part that takes a long time).

Ok Stephen last question, So

avinash1 @ 20 Oct 2010 10:00 AM

Ok Stephen last question, So if the solution is O(n) basically 2n, & if it is time limit exceeded, then is it problem with algorithm or language or chodechef environment, or something else. because further optimization looks difficult.

well thn why i am getting

lukka @ 20 Oct 2010 06:41 PM

well thn why i am getting runtime error... my program is correct and runing in my computer... i used insertion sort ... plez explain where is the problem ...i am confused

"Internal error" - is that

yuvipanda @ 21 Oct 2010 01:04 AM

"Internal error" - is that codechef buckling under load?

This is so wrong. Same

avinash1 @ 23 Oct 2010 10:42 AM

This is so wrong. Same algorithm pass the time in when written in C++ but does not pass when written in C#. It looks bound checking or extra activities ususally takes place in C#

A priority queue works like a

sbaldrich @ 30 Oct 2010 04:16 AM

A priority queue works like a charm

which input stream should i

sidcsevssut @ 31 Oct 2010 02:53 AM

which input stream should i use for taking to take input

i tried nearly all sorting algorithm but it is always showing time out error

java programmer please help

All of the sorting algorithms

triplem @ 31 Oct 2010 04:43 AM

All of the sorting algorithms you have tried so far are too slow; they run in O(n log n) time. You need something faster for this problem.

could a java programmer tell

j2ripper @ 23 Nov 2010 07:13 PM

could a java programmer tell me whats the complexity of this and if its even a good idea. im a noob at this so if u think that the reason its timing out is obvious please excuse my eh.. noobness but i think that its a pretty fast algorithm

btw i made it using inspiration from binary sort

http://www.codechef.com/views

j2ripper @ 24 Nov 2010 09:09 AM

http://www.codechef.com/viewsolution/382377 thats my code

Unfortunately, while you may

triplem @ 24 Nov 2010 09:58 AM

Unfortunately, while you may finding the position to insert the next element with a binary search in O(log n) time, you then have to move all of the elements after it forward one - and that is the part which takes all the time, as there could be n of them (n/2 on average). So the binary search ends up not actually being any more useful than searching the entire array in the first place, and your algorithm ends up with O(n^2) complexity.

There are many famous algorithms which sort numbers in O(n log n) time (try searching for some). That is the fastest complexity you can get for sorting numbers in general.

In this problem you need something even faster - use the fact that the numbers are all less than a million.

Please help me i am getting

amogha @ 24 Nov 2010 07:33 PM

Please help me i am getting run time error

#include<stdio.h>
int main()
{
long int a[100000],t,c;
register int i,j;
scanf("%d",&t);
printf("n");
for(i=1;i<=t;i++)
scanf("%d",&a[i]);
for(i=1;i<t;i++)
{
for(j=1;j<t-i+1;j++)
{
if(a[j]>=a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
printf("%dn",a[1]);
for(i=2;i<=t;i++)
{
if(a[i]==a[i-1])
continue;
printf("%dn",a[i]);
}
return 0;
}

in this case backslash is not

amogha @ 24 Nov 2010 07:34 PM

in this case backslash is not printed but its there in the profram so dont worry about that

The problem statement says t

triplem @ 25 Nov 2010 01:34 AM

The problem statement says t can be up to 1000000. You don't allow anywhere near enough space for that.

@ stephen merriman Thank you

amogha @ 27 Nov 2010 11:15 AM

@ stephen merriman Thank you however if i make my array size to 1000000 it times out why would that happen?? Could you please answer me

Because your algorithm is too

triplem @ 28 Nov 2010 02:08 AM

Because your algorithm is too slow.

No need to use arrays for

einstein010 @ 29 Nov 2010 06:21 AM

No need to use arrays for this one.. I used vectors and built in sort algorithm function..easiest to understand

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <conio.h>

using namespace std;

int main(){
int t;
scanf("%d",&t);
int n;
vector<int> result;
while(t--){
scanf("%d",&n);
result.push_back(n);
}
sort(result.begin(),result.end());
for(int i=0;i<result.size();i++){
printf("%dn",result.at(i));
}
getch();
return 0;
}

 

PS : gave time limit exceeded when i used cin and cout..so had to replace them with printf and scanf

@ Stephen Merriman Thank you

amogha @ 29 Nov 2010 05:18 PM

@ Stephen Merriman

Thank you i changed my algo and its working well now. One more question- when I write int a[1000000] is the space already alloted by the compiler or does it allot space only after initialisation?

@amogha: whenever an array is

einstein10 @ 30 Nov 2010 12:03 AM

@amogha: whenever an array is declared space is reserved for it

my submission id is

chetan_ug2k10 @ 24 Dec 2010 03:13 AM

my submission id is 406216..

i am gettin runtime error.. WHY....????

my submission id is 410257 I

phoxis @ 1 Jan 2011 06:28 PM

my submission id is 410257

I have submitted a solution for this problem, which uses the pthreads library, and i am getting a linker error. also do i need to input the unordered set from standart input ? I am currently generating the numbers randomly and sorting them. Please suggest me how can i make a successful submission for this problem.

You aren't allowed to use

triplem @ 2 Jan 2011 07:49 AM

You aren't allowed to use threads in your solution.

You must use input from standard input and write output to standard output; read the FAQ.

thanks for the help,

phoxis @ 2 Jan 2011 09:24 AM

thanks for the help, everything is clear now.

@Admin, I have solved this

thechamp @ 10 Feb 2011 08:47 PM

@Admin, I have solved this question in 2s and my solution is http://www.codechef.com/viewsolution/452897

There are solution on scoreboard which ran in 0.0s I checked solution of user 'gdkapil' (8th on scorboard) and his solution is exactly what I have done. Yet his time is 0.0s and mine 2s, what fact justifies that?

getting run time error on

bhogal08547 @ 11 Mar 2011 08:02 AM

getting run time error on submission of my cod is..

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{    int i,n,a[100000];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);

for(i=0;i<n;i++)
cout<<a[i]<<"n";
return 0;
}
please any body help me

I get "wrong answer" but in

wil93 @ 17 Mar 2011 03:55 AM

I get "wrong answer" but in my own test cases it works well. Why isn't possible to see test cases? Or just to know how many test i passed... :(

 

#include <stdio.h>

int v[1000001]={0};

int main()

{

int n,i,a;

scanf("%d",&n);

for (i=0;i<n; i++){ scanf("%d",&a); v[a]=1;  }

for (i=0;i<=1000000; i++) if (v[i]) printf("%dn",i);

return 0;

}

Ok, i did it. I saw two "5"s

wil93 @ 17 Mar 2011 04:02 AM

Ok, i did it.

I saw two "5"s in the input (and one in output) so i tought that numbers should be distinct...

Execution time: 2.19

Corrected code:

#include <stdio.h>

int v[1000001]={0};

int main()

{

int n,i,j,a;

scanf("%d",&n);

for (i=0;i<n; i++){ scanf("%d",&a); v[a]++; }

for (n=0;n<=1000000; n++) for (i=0;i<v[n];i++) printf("%dn",n);

return 0;

}


can anyone plz tell me wats

km_rama @ 30 Mar 2011 10:14 PM

can anyone plz tell me wats wrong wid this code. i cant find wats the reason for getting runtime error.
I m really fade up.
plz help me.                                                                     
                                                                     
                                                                     
                                             
#include<stdio.h>
#include<stdlib.h>

int main()
{
   unsigned long  int t,i,temp,j;
    i=0;
    int *n=calloc(1000000,2);
    scanf("%ldn",&t);
    while(t--)
    {
      scanf("%ldn",&n[i]);
      i++;
    }  
    for(i=0;i<t;i++)
      {
         for(j=0;j<t;j++)
            { 
              if(n[i]<n[j])
              {
              temp=n[i];
              n[i]=n[j];
              n[j]=temp;
              }
            }
      }
  
  for(i=0;i<t;i++)
  printf("%ldn",n[i]);
return 0;//(scanf("%ld",&i));
}               

can anyone plz tell me wats

km_rama @ 30 Mar 2011 10:27 PM

can anyone plz tell me wats wrong wid this code. i cant find wats the reason for getting runtime error.
I m really fade up.
plz help me.                                                                     
                                                                     
                                                                     
                                             
#include<stdio.h>
#include<stdlib.h>

int main()
{
   unsigned long  int t,i,temp,j;
    i=0;
    int *n=calloc(1000000,2);
    scanf("%ldn",&t);
    while(t--)
    {
      scanf("%ldn",&n[i]);
      i++;
    }  
    for(i=0;i<t;i++)
      {
         for(j=0;j<t;j++)
            { 
              if(n[i]<n[j])
              {
              temp=n[i];
              n[i]=n[j];
              n[j]=temp;
              }
            }
      }
  
  for(i=0;i<t;i++)
  printf("%ldn",n[i]);
return 0;//(scanf("%ld",&i));
}               

 

#include<stdio.h> #include<co

master_coder @ 14 Apr 2011 12:49 PM

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

void main()

{

int *a,i,j,k,t;

printf("enter the number of linesn");

scanf("%d",&t);

a=(int *)malloc(pow(10,6));

if(t<=pow(10,6))

{

for(j=0;j<t;j++)

{

scanf("%d",&a[j]);

if(a[j]<0 && a[j]>pow(10,6))

break;

}

}

for(i=0;i<t;i++)

{

for(j=i+1;j<t;j++)

{

if(a[i]>a[j])

{

k=a[i];

a[i]=a[j];

a[j]=k;

}

}

}

for(i=0;i<t;i++)

printf("%dn",a[i]);

getch();

}

The Server Test Time

henrya2 @ 19 Apr 2011 08:35 PM

The Server Test Time Meausrement is not precisely. It depend on server's load.

This times out in java any

salvo @ 4 May 2011 02:05 AM

This times out in java any suggestions i am using counting sort :(

i dont know why is this

salvo @ 4 May 2011 02:15 AM

i dont know why is this happening but a counting sort in java doesnt work but the internal implementation of qsort in C works .........
admin any guesses..........

import

salvo @ 4 May 2011 02:17 AM

import java.util.Scanner;

public class Main {
public static void main(String args[]) {
int i = 0;
int arr[] = new int[1000000];
Scanner in = new Scanner(System.in);
Integer x = new Integer(in.nextLine());
for (i = 0; i < x; i++) {
Integer num=new Integer(in.nextLine());
arr[num]++;
}
int size=0;
for(i=0;i<1000000;i++)
{
int count=arr[i];
if(count==0)
continue;
for(int j=0;j<count;j++)
{
System.out.println(i);
}
size=size+count;
if(size==arr.length)
break;
}
}

Code for counting sort

Simple, slow, yet AC solution

lethalwire @ 6 May 2011 07:37 AM

Simple, slow, yet AC solution in java:

array = new int[Integer.parseInt( br.readLine()    )];
for(int i = 0; i < array.length; ++i){
array[i] = Integer.parseInt( br.readLine() );
}
java.util.Arrays.sort(array);
for(int i: array){
sb.append(i); // sb = stringbuffer
sb.append('n');
}
System.out.println(sb.toString());

Quote from Java API about the sorting algorithm used:

Sorts the specified array of ints into ascending numerical order. [...]  This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

#include<algorithm> #include<

ronakag @ 12 May 2011 03:31 PM
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n,i;

int a[1000000];
scanf("%d",&n);
for(i=0; i<n; i++)

scanf("%d",a+i);

sort(a,a+n);

for(i=0; i<n; i++)
printf("%dn",a[i]);
return 0;
}


Though i am able to submit the correct answer the following program doesnot work in DEV C++.. Why?

coz the max size of array

ritesh_gupta @ 12 May 2011 09:26 PM

coz the max size of array depends upon the system architecture.your system doesnt have capacity to allocates memory for 10^6  integers ,while the codechef judge (system) has

thnks bro

ronakag @ 12 May 2011 10:37 PM

thnks bro

i am using Dev-cpp. the

anu_lundia @ 15 May 2011 10:21 PM

i am using Dev-cpp. the program is running fine in it but is showing runtime error here...plaease help

@ anul _lundia::your program

ritesh_gupta @ 16 May 2011 09:05 PM

@ anul _lundia::your program can never work fine on dev c++..bcoz u cant allocate int n[100000] in dev cpp.It will immidiately encounter stack overflow.Also the algo bubble sort u used wont wrk here coz the timelimit is only 5 sec.Try some other sorting algorith or usie c++ stl #include <algorithm>


can anyone please help...

harshit_sharan @ 31 May 2011 07:29 PM
can anyone please help... whats wrong with my algo here : its a perl program for quick-sort. O/P is wrong sample i/p: 6 3 8 1 6 5 4 o/p: 1 1 4 4 4 4 #!/usr/bin/perl my $t=STDIN; #not using > and < signs around stdin as then there is posting problem int($t); my @n; while($t) { push(@n,int(STDIN)); $t--; } &quick(0,$#n); sub quick { if($_[0]<$_[1]) { my $pivot = $n[$_[1]]; my $from = $_[0]; my $to = $_[1]; my $i=$from-1; my $j=$from; my $temp; while($j<$to) { if($n[$j]<=$pivot) { $i=$i+1; $temp=$n[$i]; $n[$i]=$n[$j]; $n[$j]=$temp; } $j=$j+1; } $i=$i+1; $temp=$n[$i]; $n[$i]=$pivot; $pivot=$n[$i]; &quick($from,$i-1); &quick($i+1,$to); } } foreach(@n) { print $_."n"; }

Don't just look at the word

marta9074 @ 2 Jun 2011 01:35 AM
Don't just look at the word "sort" and think of a well-known sorting algorithm. Yes, quicksort is fast but it is O(nlogn). There is an O(n) solution.

my program not providing any

sharma2856 @ 6 Jun 2011 01:51 AM
my program not providing any output ..plz any 1 help import java.util.Scanner; import java.util.Arrays; public class main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int arrayvalue= scan.nextInt(); if (arrayvalue<=1000001) { int[] el=new int[arrayvalue]; for (int elements=0; elements<=el.length; elements++) { int num=scan.nextInt(); // System.out.println(num); if (num>=0 && num<=1000001) { el[elements]=num; } } Arrays.sort(el);//method for sorting an array for (int i = 0; i < el.length; i++) { System.out.println(el[i]); } } } }

Why the solution is not

arnab_das @ 16 Jun 2011 05:21 PM
Why the solution is not getting accepted in JAVA? Here's my solution: http://www.codechef.com/viewsolution/575251 It is showing TLE every time. In my P4 2.8 ghz it takes 1.5 seconds approx. for10^6 elements. Admin plz take note...

@arnab_das: Quick Sort is

balajiganapath @ 16 Jun 2011 06:30 PM
@arnab_das: Quick Sort is very inefficient for this problem. It's average running time is O(N log N) which will time out. Use an algorithm with a better running time. Hint: Look at the constraints carefully.

but when I converted the same

arnab_das @ 16 Jun 2011 07:17 PM
but when I converted the same program to run in C, it was accepted. see: http://www.codechef.com/viewsolution/575333

Maybe its because IO is much

balajiganapath @ 16 Jun 2011 08:04 PM
Maybe its because IO is much slower in Java as compared to C/C++.

hi admin and all other, my

hungrycoder @ 17 Jun 2011 11:49 AM
hi admin and all other, my programme in c got TLE....shud it mean that it is showing correct output but not compiling within time????mean can i be sure about the correctness of my programme??

what is runtime error code

hungrycoder @ 17 Jun 2011 11:56 AM
what is runtime error code SIGSEGV????help me out please

@hundrycoder: TLE means your

balajiganapath @ 17 Jun 2011 12:24 PM
@hundrycoder: TLE means your program was compiled but failed to complete its execution within the time limit. The judge cannot decide whether it is right or wrong because your program did not finish its execution. SIGSEGV is for invalid memory reference or segmentation fault. Look it up in wikipedia. Most commonly it is because the program tried to access an array element outside the array bounds.

@balajiganapath thanks

hungrycoder @ 17 Jun 2011 02:24 PM
@balajiganapath thanks

using System; using

siva7891 @ 26 Jun 2011 10:49 AM
using System; using System.Diagnostics; namespace Turbo_Sort { class Program { static void Main( string[] args ) { int limit, i=0; limit = int.Parse(Console.ReadLine()); int[] arr = new int[limit]; Stopwatch stopwatch = new Stopwatch(); for(i=0;i

using System; using

siva7891 @ 26 Jun 2011 10:51 AM
using System; using System.Diagnostics; namespace Turbo_Sort { class Program { static void Main( string[] args ) { int limit, i=0; limit = int.Parse(Console.ReadLine()); int[] arr = new int[limit]; Stopwatch stopwatch = new Stopwatch(); for(i=0;i

Looks like code written in

hollgam @ 6 Jul 2011 09:25 PM
Looks like code written in Python always times out :(

I have submitted the

rahulreddya @ 8 Jul 2011 10:04 AM
I have submitted the following code, its working fine when i tested on my pc but the online judge is throwing "wrong answer" message. Can anyone kindly point the mistake. Thanks in advance... ============== #include #define RANGE 1000001 int main() { int index,i,tot,cnt; short int arr[RANGE]; for(i=0;i

Hey could anyone solve it

flexicoder @ 13 Jul 2011 09:48 AM
Hey could anyone solve it with quicksort...or even randomized version of it...???

Why the people are trying to

douglas @ 23 Jul 2011 08:10 AM
Why the people are trying to solve this problem with sort algorithm? It's not the best way to solve this problem.

//why time limit

tango @ 23 Jul 2011 07:05 PM
//why time limit exceed? #include #include int main() { int *p; p=(int*)malloc(1000001*sizeof(int)); int i,j,k,n,temp; scanf("%d",&n); for(k=0;kp[j]) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } for(i=0;i

#include int main() { int

laasya @ 4 Aug 2011 01:19 PM
#include int main() { int i,j=0,temp,a[20],t; scanf("n%d",&t); for(i=0;ia[j]){ temp=a[j]; a[j]=a[i]; a[i]=temp; } } } for(i=0;i

#include int main() { int

laasya @ 4 Aug 2011 01:20 PM
#include int main() { int i,j=0,temp,a[20],t; scanf("n%d",&t); for(i=0;ia[j]){ temp=a[j]; a[j]=a[i]; a[i]=temp; } } } for(i=0;i

This problem is somehow so

handra @ 22 Aug 2011 10:16 AM
This problem is somehow so strange. I submitted my application in Java and it returned me with Wrong Answer. I then convert my Java code to C, and it returned me with Accepted. Hhmm... Don't know where the wrong part is.... If any of you can find anything wrong, either with my code, or having similar experience, please do let me know. Below are the links for my source code. The first one using java, and the second one using C. Source in Java: http://www.codechef.com/viewsolution/633234 Source in C: http://www.codechef.com/viewsolution/633268

@admin //this program is

akp616 @ 28 Aug 2011 12:43 AM
@admin //this program is working well (it shows the bugs in your system) #include int main(void) { int T,i=0,arr[1000000]={0},x; scanf("%d",&T); for(;i

my program is working

rups @ 3 Sep 2011 03:00 PM
my program is working correctly in with my compiler. can ny1 plz tell why is it showing WRONG ANSWER here... #include /*TSORT*/ int main() { int t,i,x,min,max; char c[1000002]; scanf("%d",&t); for(i=0;i<=1000000;i++) c[i]='0'; scanf("%d",&x);c[x]='1'; min=max=x; for(i=2;i<=t;i++) { scanf("%d",&x); c[x]=c[x]+1; if(xmax) max=x; } for(i=min;i<=max;i++) { if(c[i]!='0') { x=c[i]-'0'; while(x>0) {printf("%dn",i);--x;} } } return 0; }

can any1 plz tell me why is

rups @ 3 Sep 2011 03:03 PM
can any1 plz tell me why is it showing WRONG RESULT for the solution no:644717 though its is working correctly with my compiler

672171 Why runtime

s1aurabhpal_7 @ 24 Sep 2011 03:57 PM
672171 Why runtime error?? plz help

anyone tell me wots wrong??

hassan @ 24 Sep 2011 04:27 PM
anyone tell me wots wrong?? It is giving runtime error?? namespace TurboSort { class Program { static void Main(string[] args) { int temp; int t = System.Convert.ToInt32(System.Console.ReadLine()); int[] n = new int[t]; for(temp=0;temp

namespace TurboSort {

hassan @ 24 Sep 2011 04:27 PM
namespace TurboSort { class Program { static void Main(string[] args) { int temp; int t = System.Convert.ToInt32(System.Console.ReadLine()); int[] n = new int[t]; for(temp=0;temp

plz help me.i used

rokon1cuet_51 @ 1 Oct 2011 10:51 AM
plz help me.i used quicksort.here i post my solution link. http://www.codechef.com/viewsolution/678694

My absolutely primitive

jelu @ 11 Oct 2011 09:24 PM
My absolutely primitive solution passes every test in Java

Talk about lucky solved in

sushantrocks @ 15 Oct 2011 06:09 PM
Talk about lucky solved in 4.99 seconds

Plz anyone tell me wats

elmagnifico @ 16 Oct 2011 12:48 PM
Plz anyone tell me wats wrong.... it's giving fine results with all test cases i tried on Dev C++ Here's my code:: #include #include #include int compare(const void *a,const void *b) { return(*(int*)a-*(int*)b); } int main() { int t,i,j,temp,z;t=i=j=temp=0;z=pow(10,6)+1; scanf("%d",&t);int a[t];int b[t]; for(i=0;i

it gives me a runtime error

garv24 @ 25 Oct 2011 11:41 PM
it gives me a runtime error with d usage of 1.9M but it accepts code with mem usage upto 100M....

@garv24: 0<=N<=10^6

imnewcoder @ 27 Oct 2011 12:44 AM
@garv24: 0<=N<=10^6

On my computer if I run the

a_ravi @ 1 Nov 2011 11:48 PM
On my computer if I run the program as time ./a.out < input > abc real 0m0.626s user 0m0.604s sys 0m0.016s The time limits are very well satisfied, but I still get the time limit exceeded error, I can't understand why?

Can someone comment on what's

cwu9t9 @ 8 Nov 2011 10:05 AM
Can someone comment on what's wrong with http://www.codechef.com/viewsolution/728169 ? it seems to pass my test. It would also be useful to know what the inputs are to the test when there is a runtime error. thanks,

I have written the program in

shaukat @ 13 Dec 2011 05:39 PM
I have written the program in python but it is showing error. Can you please help me? Following is the code: 1. #!/usr/bin/python 2. L= [] 3. count = 0 4. while (count < 9): 5. var = input("Enter something: ") 6. L.append(var) 7. count = count + 1 8. L.sort() 9. for x in L: # Second Example 10. print x 11.

can someone comment on my

shaukat @ 14 Dec 2011 03:44 PM
can someone comment on my above code in python ?

sigh.. Runtime error got me

anirudhacoder @ 16 Jan 2012 06:37 PM
sigh.. Runtime error got me really pissed off........

It got you pissed off? Man

trambilas1244 @ 16 Jan 2012 07:53 PM
It got you pissed off? Man that made me want to pull off all my hair

my heapsort code works well

maynard92 @ 24 Jan 2012 10:11 PM
my heapsort code works well in my machine but unfortunately codechef shows run-time error.... help plz

i solved the problem with

sarath23 @ 28 Jan 2012 12:29 PM
i solved the problem with quick sort. But it is showing time limit. May i know wat might be the reason

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

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 computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various 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 judge accepts solutions in over 35+ programming languages. Online programming was 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 competitions 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 programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

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. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

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