/**
Given a array of both positive and negative integers ‘arr[]’ which are sorted. Task is to sort square of the numbers of the Array.
Examples:
Input : arr[] = {-6, -3, -1, 2, 4, 5}
Output : 1, 4, 9, 16, 25, 36
Input : arr[] = {-5, -4, -2, 0, 1}
Output : 0, 1, 4, 16, 25
*/
import java.util.*;
public class SortSquaresofSortedArray{
public static int[] sort(int[] nums){
//base cases
if(nums==null || nums.length<1){
return nums;
}
int peak = 0;
//find the peak index
while(peak< nums.length && nums[peak] < 0){
peak++;
}
//if peak reaches the end, the array is already sorted in proper order
//if the array contains both positive and negative numbers, then the left part is sorted with negative numbers and the right part is sorted with positive numbers
int[] res = new int[nums.length];
//left portion is 0 to peak-1, and right portion is peak to nums.length-1
//left ends at peak-1 because we initialized peak to 0
int left = peak-1, right = peak, index = 0;
while(left>=0 && right<nums.length){
if(Math.abs(nums[left])<Math.abs(nums[right])){
res[index++] = nums[left] * nums[left];
left--;
}else{
res[index++] = nums[right] * nums[right];
right++;
}
}
//one or both halves are exhausted
while(left>=0){
res[index++] = nums[left] * nums[left];
left--;
}
while(right<nums.length){
res[index++] = nums[right] * nums[right];
right++;
}
return res;
}
public static void main(String[] args){
int[] nums = {-6, -3, -1, 2, 4, 5};
int[] res = sort(nums);
System.out.println(Arrays.toString(res));
System.out.println(Arrays.toString(sort(new int[]{-5, -4, -2, 0, 1})));
}
}
Given a array of both positive and negative integers ‘arr[]’ which are sorted. Task is to sort square of the numbers of the Array.
Examples:
Input : arr[] = {-6, -3, -1, 2, 4, 5}
Output : 1, 4, 9, 16, 25, 36
Input : arr[] = {-5, -4, -2, 0, 1}
Output : 0, 1, 4, 16, 25
*/
import java.util.*;
public class SortSquaresofSortedArray{
public static int[] sort(int[] nums){
//base cases
if(nums==null || nums.length<1){
return nums;
}
int peak = 0;
//find the peak index
while(peak< nums.length && nums[peak] < 0){
peak++;
}
//if peak reaches the end, the array is already sorted in proper order
//if the array contains both positive and negative numbers, then the left part is sorted with negative numbers and the right part is sorted with positive numbers
int[] res = new int[nums.length];
//left portion is 0 to peak-1, and right portion is peak to nums.length-1
//left ends at peak-1 because we initialized peak to 0
int left = peak-1, right = peak, index = 0;
while(left>=0 && right<nums.length){
if(Math.abs(nums[left])<Math.abs(nums[right])){
res[index++] = nums[left] * nums[left];
left--;
}else{
res[index++] = nums[right] * nums[right];
right++;
}
}
//one or both halves are exhausted
while(left>=0){
res[index++] = nums[left] * nums[left];
left--;
}
while(right<nums.length){
res[index++] = nums[right] * nums[right];
right++;
}
return res;
}
public static void main(String[] args){
int[] nums = {-6, -3, -1, 2, 4, 5};
int[] res = sort(nums);
System.out.println(Arrays.toString(res));
System.out.println(Arrays.toString(sort(new int[]{-5, -4, -2, 0, 1})));
}
}
No comments:
Post a Comment