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
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.