AlgorithmsData StructuresContact
Sorted Square Array
Rami Del Toro
Rami Del Toro
August 24, 2021
12 min

Table Of Contents

01
Overview
02
Flow Diagram - Brute Force
03
Time Complexity
04
Space Complexity
05
Java Source Code
06
Solution - Brute Force
07
Flow Diagram - Optimal
08
Java Source Code
09
Solution - Optimal
10
Conclusion
Sorted Square Array

Given an integer array sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Overview

Sorted Square Algorithm Overview Diagram
Sorted Square Algorithm Overview Diagram

Need to square each element and save it in a new array sorted. The tricky part is when the array has negative numbers. It is worth mentioning that the input array is sorted, we will use that to our advantage.

Flow Diagram - Brute Force

Sorted Square Bruteforce Flow Diagram
Sorted Square Bruteforce Flow Diagram

We went through each element and then at the end we sorted the array. This solution also works with unsorted arrays. Since we know that the input array is sorted. There is a more optimal solution.

Time Complexity

To solve this problem, we need to iterate through the array once, that leads us to a time complexity of O(n). The input array is already sorted so it no extra complexity is accounted for.

Space Complexity

The space complexity is O(n) as a new array needs to be created to store the squared values.

Java Source Code

Solution - Brute Force

public int[] bruteForce(final int[] inputArray) {
        final var sortedSquareArray = new int[inputArray.length];

        for(int i = 0;i<inputArray.length;i++) {
            sortedSquareArray[i] = (int) Math.pow(inputArray[i],2);
        }

        Arrays.sort(sortedSquareArray);

        return sortedSquareArray;
    }

Flow Diagram - Optimal

Square Optimal Flow Diagram
Square Optimal Flow Diagram

No need to sort at the end, thus having better time complexity. The strategy here is to note:

  • Array is sorted
  • Smaller negative numbers result in higher squared number.
  • Compare the smallest negative number with the largest number. Who ever is largest gets to go into the position in the array
  • Repeat previous point until we traversed the input array

Java Source Code

Solution - Optimal

public int[] optimal(final int[] inputArray) {
            final var sortedSquareArray = new int[inputArray.length];

            var startIndex = 0;
            var endIndex = inputArray.length-1;

            for(int i = inputArray.length-1;i>=0;i--) {
                var sortedArrayElement = 0;
                if(Math.abs(inputArray[endIndex]) > Math.abs(inputArray[startIndex])) {
                    sortedArrayElement = (int) Math.pow(inputArray[endIndex],2);
                    endIndex--;
                } else {
                    sortedArrayElement = (int) Math.pow(inputArray[startIndex],2);
                    startIndex++;
                }

                sortedSquareArray[i] = sortedArrayElement;
            }

            return sortedSquareArray;
        }

Conclusion

Sometimes you need time away to sort things out in your head. It makes everything clearer

Jojo Moyes

Tags

algorithmsarrayeasysort

Related Posts

How to implement the MergeSort Algorithm
How to implement the MergeSort Algorithm
October 10, 2021
25 min
© 2022 Rami Del Toro ,All Rights Reserved.