Java Given a Sorted List of Integers Square Them and Return Them Again Sorted
Problem Statement
Given an assortment of numbers sorted in increasing gild, return a new array containing squares of all the numbers of the input array sorted in increasing order.
Example one:
Input: a[ ] = [-5, -2, -1, 0, 4, six ] Output: [ 0, i, 4, 16, 25, 36 ] Caption: After squaring, the assortment becomes [ 25, 4, i, 0, 16, 36 ]. After sorting, it becomes [ 0, 1, 4, 16, 25, 36 ]. Example 2:
Input: nums = [-5, -4, -3, -2, -1] Output: [ 1, 4, 9, xvi, 25 ] Example three:
Input: nums = [ i, two, 3, 4, v ] Output: [ one, 4, ix, 16, 25 ] Solution
A simple approach is to foursquare the array and so sort information technology to get the concluding assortment. This approach volition have an O(n*log n) time complexity.
Merely given that the assortment is sorted, we can utilize the two-pointer approach to solve it in O(north) fourth dimension-complication.
2 pointer arroyo
If the array only had positive integers, then we could merely square it and return the squared array as output. The only caveat with this question is that information technology has negative integers likewise.
But if we can view the negative part and positive office of the array separately, and iterate over both the parts then we tin can become the squared array in O(n) time complexity.
Here is how the approach volition work:
- Find the index of the showtime non-negative number in the array.
- Use 2 pointers to iterate over the array - one pointer will movement forward to iterate over the non-negative numbers, and the other pointer will move backward to iterate over the negative numbers.
- At whatever pace, we check which arrow gives a smaller square. We add together the smaller square to the output array and advance the corresponding arrow.
←pointer1 pointer2→ [-5, -two, -1, 0, 4, 6 ] course SquaresOfSortedArray { public static int [ ] sortedSquares ( int [ ] a) { int n = a.length; int [ ] squaredArr = new int [n] ; int smallestSquareIdx = 0 ; // Find the alphabetize of first non-negative chemical element int firstNonNegativeElementIndex = n; for ( int i = 0 ; i < n; i++ ) { if (a[i] >= 0 ) { firstNonNegativeElementIndex = i; break ; } } // Pointer to iterate over the negative elements int negItr = firstNonNegativeElementIndex- 1 ; // Pointer to iterate over the not-negative elements int posItr = firstNonNegativeElementIndex; while (negItr >= 0 && posItr < n) { int negElementSquare = a[negItr] *a[negItr] ; int posElementSquare = a[posItr] *a[posItr] ; if (negElementSquare < posElementSquare) { squaredArr[smallestSquareIdx++ ] = negElementSquare; negItr-- ; } else { squaredArr[smallestSquareIdx++ ] = posElementSquare; posItr++ ; } } // Add the foursquare of remaining negative elements to the output while (negItr >= 0 ) { squaredArr[smallestSquareIdx++ ] = a[negItr] *a[negItr] ; negItr-- ; } // Add the square of the remaining positive elements to the output while (posItr < northward) { squaredArr[smallestSquareIdx++ ] = a[posItr] *a[posItr] ; posItr++ ; } return squaredArr; } public static void main ( String [ ] args) { Scanner keyboard = new Scanner ( System .in) ; int n = keyboard. nextInt ( ) ; int [ ] a = new int [n] ; for ( int i = 0 ; i < n; i++ ) { a[i] = keyboard. nextInt ( ) ; } keyboard. close ( ) ; int [ ]squaredArray = sortedSquaresSimplified (a) ; for ( int i = 0 ; i < squaredArray.length; i++ ) { System .out. print (squaredArray[i] + " " ) ; } System .out. println ( ) ; } } Ii pointer approach simplified
An alternate 2 pointer approach is to utilize two pointers - one starting from the rightmost element of the assortment and the other starting from the leftmost element.
pointer1→ ←pointer2 [-5, -2, -1, 0, 4, 6 ] At every step, check which pointer gives the highest square. Add the highest foursquare to the output array (starting from correct) and advance the corresponding pointer.
form SquaresOfSortedArray { public static int [ ] sortedSquaresSimplified ( int [ ] a) { int n = a.length; int [ ] squaredArr = new int [n] ; int highestSquareIdx = n- one ; int left = 0 ; int right = n- i ; while (left <= right) { int leftSquare = a[left] *a[left] ; int rightSquare = a[right] *a[right] ; if (leftSquare > rightSquare) { squaredArr[highestSquareIdx-- ] = leftSquare; left++ ; } else { squaredArr[highestSquareIdx-- ] = rightSquare; right-- ; } } return squaredArr; } public static void main ( String [ ] args) { Scanner keyboard = new Scanner ( System .in) ; int n = keyboard. nextInt ( ) ; int [ ] a = new int [n] ; for ( int i = 0 ; i < n; i++ ) { a[i] = keyboard. nextInt ( ) ; } keyboard. close ( ) ; int [ ]squaredArray = sortedSquaresSimplified (a) ; for ( int i = 0 ; i < squaredArray.length; i++ ) { System .out. print (squaredArray[i] + " " ) ; } Arrangement .out. println ( ) ; } } tylorhicaltisee38.blogspot.com
Source: https://www.callicoder.com/squares-of-a-sorted-array/
Post a Comment for "Java Given a Sorted List of Integers Square Them and Return Them Again Sorted"