• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Bartender
Posts: 5560
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 669
15
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5560
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing your insights, Piet. Very useful!
 
Piet Souris
Bartender
Posts: 5560
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic