---
# System prepended metadata

title: Untitled

---

[-5,-3,-1, 0, 2, 4, 7]

[0, 1, 4, 9, 16, 25, 49]

[-1, -3, -5]


```java=
int[] sortedBySquare(int[] inputs) {
    int[] results = new int[inputs.length];
    List<Integer> negatives = new ArrayList<>();
    List<Integer> positives = new ArrayList<>();
    
    for (int i = 0; i < inputs.length; i++) {
        if (inputs[i] >= 0) {
            positives.add(inputs[i]);
        } else {
            negatives.add(inputs[i]);
        }                                                       
    }
    
    int posIndex = 0;
    int negIndex = negatives.size()-1;
    for (int i = 0; i < inputs.length; i++) {
        if (negIndex < 0) {
            for (int j = posIndex; j < positives.size(); j++) 
                result[i+j-posIndex] = positives.get(j) * positives.get(j);
            break;
        }
        if (posIndex == positives.size()) {
            for (int j = negIndex; j >= 0; j--) 
                result[i-j+negIndex] = negatives.get(j) * negatives.get(j);
            break;
        }
        if (positives.get(posIndex) > -negatives.get(negIndex)) {
            results[i] = negatives.get(negIndex)*negatives.get(negIndex);
            negIndex--;
        } else {
            results[i] = positives.get(posIndex)*positives.get(posIndex);
            posIndex++;
        }
    }
    
    return results;
}

int binarySearch(int[] inputs, int startIndex, int endIndex) {
    if (startIndex == endIndex) return startIndex;
    if (endIndex - startIndex == 1 && inputs[startIndex] >= 0) return startIndex;
    if (endIndex - startIndex == 1 && inputs[startIndex] < 0) return endIndex; 
    int midIndex = (endIndex - startIndex)/2 + startIndex;
    
    if (inputs[midIndex] >= 0) return binarySearch(inputs, startIndex, midIndex);
    else return binarySearch(inputs, midIndex, endIndex);
}

source = "abcc", target = "abccb"
output = 2
targetBuild 
a  bc    
b  c
c
c  
b  c
```