`{"id":"f4519adf-6b20-4a28-8c87-1f4c5f0e37b2","name":"Squares Of A Sorted Array","description":"1. Given an integer array 'nums' sorted in non-decreasing order.\r\n2. Return an array of the squares of each number sorted in non-decreasing order.\r\n","inputFormat":"nums = [-4,-1,0,3,10]\r\n","outputFormat":"[0,1,9,16,100]\r\nExplanation: After squaring, the array becomes [16,1,0,9,100].\r\nAfter sorting, it becomes [0,1,9,16,100].\r\n","constraints":"1. 1 &lt;= nums.length &lt;= 10^4\r\n2. -10^4 &lt;= nums[i] &lt;= 10^4\r\n3. 'nums' is sorted in non-decreasing order.\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<int> sortedSquares(vector<int> &nums) {\n // write your code here\n return null;\n}\n\nint main() {\n int n = 0;\n cin>>n;\n vector<int> nums(n);\n \n for(int i = 0; i < n; i++) \n cin>>nums[i];\n \n vector<int> res = sortedSquares(nums);\n\n for(int i = 0; i < n; i++) {\n cout<<res[i]<<\" \";\n }\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~~~~\r\n\r\n public static int[] sortedSquares(int[] nums) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~~~\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] nums = new int[n];\r\n \r\n for(int i = 0; i < n; i++) \r\n nums[i] = scn.nextInt();\r\n \r\n int[] res = sortedSquares(nums);\r\n\r\n for(int i = 0; i < n; i++) {\r\n System.out.print(res[i] + \" \");\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"9\r\n-6 -2 -1 0 1 4 5 7 9","sampleOutput":"0 1 1 4 16 25 36 49 81","questionVideo":"https://www.youtube.com/embed/u3A64HQq_Dw","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"bc6cc467-1d5b-4ad2-ba11-c0fb2cd40a67","name":"Squares Of A Sorted Array","slug":"squares-of-a-sorted-array","type":1}],"next":{"id":"12364f80-c6df-4cb7-b510-e5f1d202d946","name":"Squares of a sorted array","type":3,"slug":"squares-of-a-sorted-array"},"prev":{"id":"528b6a36-e1cd-47b0-95a7-1bb2b9b29270","name":"Min Jumps With +i -i Moves MCQ","type":0,"slug":"min-jumps-with-i-i-moves-mcq"}}}`

# Squares Of A Sorted Array

1. Given an integer array 'nums' sorted in non-decreasing order. 2. Return an array of the squares of each number sorted in non-decreasing order.

`{"id":"f4519adf-6b20-4a28-8c87-1f4c5f0e37b2","name":"Squares Of A Sorted Array","description":"1. Given an integer array 'nums' sorted in non-decreasing order.\r\n2. Return an array of the squares of each number sorted in non-decreasing order.\r\n","inputFormat":"nums = [-4,-1,0,3,10]\r\n","outputFormat":"[0,1,9,16,100]\r\nExplanation: After squaring, the array becomes [16,1,0,9,100].\r\nAfter sorting, it becomes [0,1,9,16,100].\r\n","constraints":"1. 1 &lt;= nums.length &lt;= 10^4\r\n2. -10^4 &lt;= nums[i] &lt;= 10^4\r\n3. 'nums' is sorted in non-decreasing order.\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvector<int> sortedSquares(vector<int> &nums) {\n // write your code here\n return null;\n}\n\nint main() {\n int n = 0;\n cin>>n;\n vector<int> nums(n);\n \n for(int i = 0; i < n; i++) \n cin>>nums[i];\n \n vector<int> res = sortedSquares(nums);\n\n for(int i = 0; i < n; i++) {\n cout<<res[i]<<\" \";\n }\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~~~~\r\n\r\n public static int[] sortedSquares(int[] nums) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~~~\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] nums = new int[n];\r\n \r\n for(int i = 0; i < n; i++) \r\n nums[i] = scn.nextInt();\r\n \r\n int[] res = sortedSquares(nums);\r\n\r\n for(int i = 0; i < n; i++) {\r\n System.out.print(res[i] + \" \");\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"9\r\n-6 -2 -1 0 1 4 5 7 9","sampleOutput":"0 1 1 4 16 25 36 49 81","questionVideo":"https://www.youtube.com/embed/u3A64HQq_Dw","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"bc6cc467-1d5b-4ad2-ba11-c0fb2cd40a67","name":"Squares Of A Sorted Array","slug":"squares-of-a-sorted-array","type":1}],"next":{"id":"12364f80-c6df-4cb7-b510-e5f1d202d946","name":"Squares of a sorted array","type":3,"slug":"squares-of-a-sorted-array"},"prev":{"id":"528b6a36-e1cd-47b0-95a7-1bb2b9b29270","name":"Min Jumps With +i -i Moves MCQ","type":0,"slug":"min-jumps-with-i-i-moves-mcq"}}}`

Editor

# Squares Of A Sorted Array

easy

1. Given an integer array 'nums' sorted in non-decreasing order. 2. Return an array of the squares of each number sorted in non-decreasing order.

## Constraints

1. 1 <= nums.length <= 10^4 2. -10^4 <= nums[i] <= 10^4 3. 'nums' is sorted in non-decreasing order.

## Format

### Input

nums = [-4,-1,0,3,10]

### Output

[0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100].

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}9 -6 -2 -1 0 1 4 5 7 9```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}0 1 1 4 16 25 36 49 81`

Question Video

Discussions

Show Discussion

Related Resources