given an array, for each element, find out the total number of elements lesser than it, which appear to the right of it

后端 未结 10 1804
傲寒
傲寒 2021-02-04 11:43

I had previously posted a question, Given an array, find out the next smaller element for each element now, i was trying to know , if there is any way to find out \"given an ar

10条回答
  •  梦毁少年i
    2021-02-04 12:11

    Another approach without using the tree.

    1. Construct another sorted array . For example for input array {12, 1, 2, 3, 0, 11, 4} it will be {0, 1, 2, 3, 4, 11, 12}
    2. Now compare position of each element from input array with sorted array.For example 12 in first array is at 0 index while sorted array it’s as 6
    3. Once comparison is done, remove element from both array

提交回复
热议问题