GeeksforGeeks » Interview Questions

Special Sorting

(2 posts)
  1. udp
    guest
    Posted 6 months ago #

    Input: A unsorted array of size n.
    Output: An array of size n.

    Relationship:

    > elements of input array and output array have 1:1 correspondence.
    > output[i] is equal to the input[j] (j>i) which is smaller than input[i] and jth is nearest to ith ( i.e. first element which is smaller).
    > If no such element exists for Input[i] then output[i]=0.

    Eg.
    Input: 1 5 7 6 3 16 29 2 7
    Output: 0 3 6 3 2 2 2 0 0

  2. kartik
    Moderator
    Posted 6 months ago #

    It's a variation of next greater element problem (http://www.geeksforgeeks.org/archives/8405). Instead of next greater element, this problem asks about next smaller element.


Reply

You must log in to post.

RSS feed for this topic