Skip to content

Commit

Permalink
Adding 0977-squares-of-a-sorted-array.java solution
Browse files Browse the repository at this point in the history
  • Loading branch information
vorenusCoA committed Feb 6, 2023
1 parent 66ab0a3 commit c24618b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions java/0977-squares-of-a-sorted-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {

public int[] sortedSquares(int[] nums) {

int[] result = new int[nums.length];

int right = nums.length - 1;
int left = 0;
int resultIndex = result.length - 1;
while (left <= right) {

if (nums[left] * nums[left] >= nums[right] * nums[right]) {
result[resultIndex] = nums[left] * nums[left];
left++;
} else {
result[resultIndex] = nums[right] * nums[right];
right--;
}
resultIndex--;
}

return result;
}
}

0 comments on commit c24618b

Please sign in to comment.