Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Square sorted array Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Square sorted array | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array of numbers, return an array that contains the squares of all the numbers in non-decreasing order.

Naive Approach

If we change all the elements in the array to their absolute value, this problem can be solved easily. All that needs to be done now is to take the smallest element of the array one by one, square it and insert it in a new array.

Analysis

  • Time Complexity: O(n2)
  • Space Complexity: O(n)

Implementation

C++
vector<int> getSquareSortedArray(vector<int> &arr) {
   int n = arr.size();
   for (int i = 0; i < n; i++) {
       arr[i] = abs (arr[i]);
   }
   vector<int> ans;
   for( int i = 0; i < n; i++) {
       int currentMinimum = INT_MAX;
       int indx;
       for (int j = 0; j < n ; j++) {
           if (arr[j] < currentMinimum) {
               currentMinimum = arr[j];
               indx = j;
           }
       }
       arr[indx] = INT_MAX;
       ans.push_back(currentMinimum * currentMinimum);
   }
   return ans;
}
Java
class Solution {
   int[] getSquareSortedArray (int[] arr) {
       int n = arr.length;
       for (int i = 0; i < n; i++) {
           arr[i] = Math.abs (arr[i]);
       }
       int[] ans = new int[n];
       for( int i = 0; i < n; i++) {
           int currentMinimum = Integer.MAX_VALUE;
           int indx = -1;
           for (int j = 0; j < n ; j++) {
               if (arr[j] < currentMinimum) {
                   currentMinimum = arr[j];
                   indx = j;
               }
           }
           arr[indx] = Integer.MAX_VALUE;
           ans[i] = (currentMinimum * currentMinimum);
       }
       return ans;
   }
}

Optimal Approach

In this problem, the optimal approach is pretty simple and straightforward. First, all elements of the array can be squared and then sorted.

Analysis

  • Time Complexity: O(n * log(n))
  • Space Complexity: O(1)

Implementation

C++
vector<int> getSquareSortedArray(vector<int> &arr) {
   for (int i = 0; i < arr.size(); i++) {
       arr[i] *= arr[i];
   }
   sort (arr.begin(), arr.end());
   return arr;
}
Java
class Solution {
   int[] getSquareSortedArray (int[] arr) {
       for (int i = 0; i < arr.length; i++) {
           arr[i] *= arr[i];
       }
       Arrays.sort(arr);
       return arr;
   }
}
Related Content
Arithmetic Sequence
Implement Counting Sort
Implement Insertion Sort
Implement Merge Sort
Implement Quicksort
Inversion Count
Kth Largest Element
Merge Overlapping Intervals
Merge Sorted Subarrays
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet