565.Create and Fill an Array of Squares
Easy
Given
n, create and return an array of length n whose element at index i is (i + 1)² — the squares 1, 4, 9, …, n².
An arrayArrayA fixed-length sequence of values of the same type, stored side by side and accessed by index starting at 0. holds a sequence of values that you reach by position, starting from index 0Zero-Based IndexingCounting positions from 0, so the first element is at index 0 and the last at index length − 1.. There are two everyday ways to build one: create it at its final size and fill each slot, or start empty and append values one at a time. Printing the result (as you would in a real program) is just a loop over the finished array.
Example 1:
Input: n = 5
Output: [1,4,9,16,25]
Example 2:
Input: n = 1
Output: [1]
+ 3 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ n ≤ 1000
n =
5