Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add createTargetArray leetcode question assignment2 #4

Merged
merged 1 commit into from Nov 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions assignment2/createTargetArray.java
@@ -0,0 +1,35 @@
package assignment2;

import java.util.ArrayList;
import java.util.Collections;

public class createTargetArray {
public static void main(String[] args) {
int[] nums = {-7,-3,2,3,11};

int front = 0;
int mid = ((0)+nums.length-1)/2;
int end = nums.length-1;

ArrayList<Integer> list = new ArrayList<>();

while(end >= mid && front <= mid) {
if (Math.abs(nums[front]) <= Math.abs(nums[end])) {
list.add(nums[end]*nums[end]);
end--;
} else if (Math.abs(nums[front]) > Math.abs(nums[end])) {
list.add(nums[front] * nums[front]);
front++;
}
}
// list.add(end);
Collections.sort(list);

Integer[] arr = new Integer[list.size()];
arr = list.toArray(arr);

for (Integer x : arr) {
System.out.print(x + " ");
}
}
}