Leetcode - Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/
Given an array of integers A
sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
Example 1:
Input: [-4,-1,0,3,10] Output: [0,1,9,16,100]
func sortedSquares(A []int) []int { result := make([]int, len(A)) start := 0 for start < len(A) && A[start] < 0 { start++ } left := start - 1 right := start index := 0 for left >= 0 && right < len(A) { leftSquare := A[left] * A[left] rightSquare := A[right] * A[right] if leftSquare < rightSquare { result[index] = leftSquare left-- } else { result[index] = rightSquare right++ } index++ } for left >= 0 { result[index] = A[left] * A[left] index++ left-- } for right < len(A) { result[index] = A[right] * A[right] index++ right++ } return result }
Jan 28, 2023 11:40:09 PM
Punjab National Bank is a popular yet most preferred National Bank in India, and the Net Banking service for the bank allows its customers to utilize different services online without investing in the branch. PNB Net Banking As the modern days are invoking with the technology, the services from Banks through their Net Banking feature are in good spike, and there are multiple benefits for the customer who does get the access to the PNB Net Banking service.