• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Your program took more time than expected.Time Limit Exceeded. Expected Time Limit < 3.496sec

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

Here, is my code link. https://ide.geeksforgeeks.org/J4DblHKLOK
When I submitted code here ( https://practice.geeksforgeeks.org/problems/immediate-smaller-element/0/?track=sp-arrays-and-searching&batchId=152 ), It shows
"Your program took more time than expected.Time Limit Exceeded
Expected Time Limit < 3.496sec
Hint : Please optimize your code and submit again."

Why "Time Limit Exceeded" occurs? How can I solve the problem?

N.B. When I run the code in ( https://ide.geeksforgeeks.org/J4DblHKLOK ), everything works fine. But when I click the "Submit" button here ( https://practice.geeksforgeeks.org/problems/immediate-smaller-element/0/?track=sp-arrays-and-searching&batchId=152 ), Then It shows following error "Your program took more time than expected.Time Limit Exceeded
Expected Time Limit < 3.496sec
Hint : Please optimize your code and submit again."

Thanks.
 
Master Rancher
Posts: 3313
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't see a particular problem, so I ran it and it says it ran in 0.08 seconds: https://ide.geeksforgeeks.org/JugtmIcr1X

I don't know why you got the message you did.  Maybe the server was having problems at that point?  If you run it again now, does it work in time?
 
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same here. I also ran it without any issue (in 0.06 sec)
 
Nahid alam
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:I didn't see a particular problem, so I ran it and it says it ran in 0.08 seconds: https://ide.geeksforgeeks.org/JugtmIcr1X

I don't know why you got the message you did.  Maybe the server was having problems at that point?  If you run it again now, does it work in time?



Thanks for your answer. When I run the code in ( https://ide.geeksforgeeks.org/J4DblHKLOK ), everything works fine. But when I click the "Submit" button here ( https://practice.geeksforgeeks.org/problems/immediate-smaller-element/0/?track=sp-arrays-and-searching&batchId=152 ), Then It shows following error "Your program took more time than expected.Time Limit Exceeded
Expected Time Limit < 3.496sec
Hint : Please optimize your code and submit again."
 
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But don't forget the array can be as big as 10^7. Try to print 10M ints to your console, and measure the time. It reminds me of a similar exercise at HackerRank, where I had to print about 10M strings to the console. Whatever I tried, I kept having these TimeOut errors. At last, I changed the complete given inputroutine. Here you use a split and parseInt (java speak) to get your array, See if you can replace that with something faster.
 
Rancher
Posts: 478
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code may run fine with the example test cases but it  probably doesn't hold up under all the test cases that are tested when you submit.  You will probably have to improve it.
 
Al Hobbs
Rancher
Posts: 478
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the input will always be a number you dont even have to int.parse.  you can just compare the characters
 
Piet Souris
Saloon Keeper
Posts: 3443
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget that 2 < 100, but "2" > "100".

First improvement: how many times do you 'int.Parse' an element of the array?
If that doesn't help: you are calling Console.Write for every element in the array. What if you write them to (in Java speak) a StringBuilder first, and print that when you're done?

Another way I considered was to compare two strings first by looking at their size, and then doing the parsing, if necessary. But the elements are between 1 and 1000, so in many cases you still have to use parsing. I haven't tested it, but it may be worth trying.
 
Brecht Geeraerts
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sharing your insights, Piet. Very useful!
 
Piet Souris
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Brecht.

@Al
excuses, I misunderstood you. I've never compared two Strings by comparing character by character. Maybe that is indeed faster than parsing. It certainly is worth testing.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!