Leetcode in Java && Oracle
419 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-11-09_18-00-09.png
60.1 KB
#N1122. Relative Sort Array
problem link

#solution
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count=new int[1001];
int[] res=new int[arr1.length];

for(int i=0; i<arr1.length; i++){
count[arr1[i]]++;
}
int index=0;
for(int i=0; i<arr2.length; i++){
while(count[arr2[i]] > 0){
res[index]=arr2[i];
index++;
count[arr2[i]]--;
}
}

for(int i=0; i<count.length; i++){
if(count[i]!=0){
while(count[i]>0){
res[index]=i;
index++;
count[i]--;
}
}
}

return res;
}
}