InterviewBit Academy is now Scaler!
Learn Tech Skills from Scratch @ Scaler EDGE

Sort array with squares!


Problem Description

Given a sorted array A containing N integers both positive and negative.

You need to create another array containing the squares of all the elements in A and return it in non-decreasing order.

  • Try to this in O(N) time.



  • Problem Constraints

    1 <= N <= 105.

    -103 <= A[i] <= 103



    Input Format

    First and only argument is an integer array A.



    Output Format

    Return a integer array as described in the problem above.



    Example Input

    Input 1:

     A = [-6,- 3, -1, 2, 4, 5]
    

    Input 2:

     A = [-5, -4, -2, 0, 1]
    


    Example Output

    Output 1:

     [1, 4, 9, 16, 25, 36]
    

    Output 2:

     [0, 1, 4, 16, 25]
    


    NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
    Start solving Sort array with squares! on Interview Code Editor
    Sign Up
    to access hints and editorial solutions for Sort array with squares!
    Asked In:

    Discussion


    Loading...
    Click here to start solving coding interview questions