import java.util.*;
public class CountofSmallerNumbersAfterSelf{
int []count;
public int[] countSmaller(int[] nums) {
count = new int[nums.length];
int []indexes = new int[nums.length];
for(int i = 0; i < nums.length; i++){
indexes[i] = i;
}
mergesort(nums, indexes, 0, nums.length - 1);
for(int i:count)
System.out.println(i);
return count;
}
private void mergesort(int[] nums, int[] indexes, int start, int end){
if(end <= start){
return;
}
int mid = (start + end) / 2;
mergesort(nums, indexes, start, mid);
mergesort(nums, indexes, mid + 1, end);
merge(nums, indexes, start, end);
}
private void merge(int[] nums, int[] indexes, int start, int end){
int mid = (start + end) / 2;
int left_index = start;
int right_index = mid+1;
int rightcount = 0;
int[] new_indexes = new int[nums.length];
int sort_index = start;
while(left_index <= mid && right_index <= end){
if(nums[indexes[right_index]] < nums[indexes[left_index]]){
new_indexes[sort_index++] = indexes[right_index++];
rightcount++;
}else{
count[indexes[left_index]] += rightcount;
new_indexes[sort_index++] = indexes[left_index++];
}
}
while(left_index <= mid){
count[indexes[left_index]] += rightcount;
new_indexes[sort_index++] = indexes[left_index++];
}
while(right_index <= end){
new_indexes[sort_index++] = indexes[right_index++];
}
for(int i = start; i <= end; i++){
indexes[i] = new_indexes[i];
}
}
public static void main(String []args){
int []nums={5,2,6,1};
CountofSmallerNumbersAfterSelf ct=new CountofSmallerNumbersAfterSelf();
ct.countSmaller(nums);
}
}
/*
You are given an integer array nums and you have to return a new counts array.
The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
Given nums = [5, 2, 6, 1]
To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.
[2,1,1,0]
*/
import java.util.ArrayList;
import java.util.Collections;
public class CountofSmallerNumbersII{
public class TreeNode{
int val;
int num;
TreeNode lchild;
TreeNode rchild;
public TreeNode(int val){
this.val=val;
this.num=0;
}
}
public ArrayList<Integer> countSmaller(int []nums){
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(0);
TreeNode root=new TreeNode(nums[nums.length-1]);
root.num=1;
for(int i=nums.length-2;i>=0;i--){
list.add(getIndex(root,nums[i],0));
}
Collections.reverse(list);
for(Integer i:list){
System.out.println(i);
}
return list;
}
public int getIndex(TreeNode root,int val,int num){
if(root.val>=val){
root.num+=1;
if(root.lchild==null){
TreeNode node=new TreeNode(val);
node.num=1;
root.lchild=node;
return num;
}else{
return getIndex(root.lchild,val,num);
}
}else{
num+=root.num;
if(root.rchild==null){
TreeNode node=new TreeNode(val);
node.num=1;
root.rchild=node;
return num;
}else{
return getIndex(root.rchild,val,num);
}
}
}
public static void main(String []args){
int []nums={5,2,6,1};
CountofSmallerNumbersII cs=new CountofSmallerNumbersII();
cs.countSmaller(nums);
}
}