Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Fill numbers from 1 to n,fast algorithm

I need algorithm what can fill string/array/etc by numbers from 1 to n; 1 <= 1 <= 10^8. Time limit ≈ 2 seconds.

n = 100000000;
arr = [];
for (let i = 1;i <= n;i++){
    arr.push(i)
}
console.log(arr)

If try like this it takes 33.785 seconds that too much

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You make multiple reallocation of memory for string - this is rather long procedure.

If possible - preset list/array capacity, then fill it with values, rather than increment length one-by-one. In Python you can use list comprehension - perhaps JS has something alike.

For strings use a kind of StringBuilder if available.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!