📜  在右侧找到最远的较小数字

📅  最后修改于: 2021-05-06 18:37:48             🧑  作者: Mango

给定大小为N的数组arr [] 。对于数组中的每个元素,任务是找到数组中最右边的元素的索引,该索引小于当前元素。如果不存在这样的数字,则打印-1

例子:

方法:一种有效的方法是创建一个suffix_min []数组,其中suffix_min [i]存储子数组arr [i…N – 1]中的最小元素。现在,对于任何元素arr [i] ,都可以在子数组suffix_min [i + 1…N – 1]上使用二进制搜索,以找到arr [i]右边最远的最小元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the farthest
// smaller number in the right side
void farthest_min(int a[], int n)
{
    // To store minimum element
    // in the range i to n
    int suffix_min[n];
    suffix_min[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        suffix_min[i] = min(suffix_min[i + 1], a[i]);
    }
 
    for (int i = 0; i < n; i++) {
        int low = i + 1, high = n - 1, ans = -1;
 
        while (low <= high) {
            int mid = (low + high) / 2;
 
            // If currnet element in the suffix_min
            // is less than a[i] then move right
            if (suffix_min[mid] < a[i]) {
                ans = mid;
                low = mid + 1;
            }
            else
                high = mid - 1;
        }
 
        // Print the required answer
        cout << ans << " ";
    }
}
 
// Driver code
int main()
{
    int a[] = { 3, 1, 5, 2, 4 };
    int n = sizeof(a) / sizeof(a[0]);
 
    farthest_min(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to find the farthest
    // smaller number in the right side
    static void farthest_min(int[] a, int n)
    {
        // To store minimum element
        // in the range i to n
        int[] suffix_min = new int[n];
 
        suffix_min[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            suffix_min[i]
                = Math.min(suffix_min[i + 1], a[i]);
        }
 
        for (int i = 0; i < n; i++) {
            int low = i + 1, high = n - 1, ans = -1;
 
            while (low <= high) {
                int mid = (low + high) / 2;
 
                // If currnet element in the suffix_min
                // is less than a[i] then move right
                if (suffix_min[mid] < a[i]) {
                    ans = mid;
                    low = mid + 1;
                }
                else
                    high = mid - 1;
            }
 
            // Print the required answer
            System.out.print(ans + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] a = { 3, 1, 5, 2, 4 };
        int n = a.length;
 
        farthest_min(a, n);
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
 
# Function to find the farthest
# smaller number in the right side
 
 
def farthest_min(a, n):
 
    # To store minimum element
    # in the range i to n
    suffix_min = [0 for i in range(n)]
    suffix_min[n - 1] = a[n - 1]
    for i in range(n - 2, -1, -1):
        suffix_min[i] = min(suffix_min[i + 1], a[i])
 
    for i in range(n):
        low = i + 1
        high = n - 1
        ans = -1
 
        while (low <= high):
            mid = (low + high) // 2
 
            # If currnet element in the suffix_min
            # is less than a[i] then move right
            if (suffix_min[mid] < a[i]):
                ans = mid
                low = mid + 1
            else:
                high = mid - 1
 
        # Print the required answer
        print(ans, end=" ")
 
 
# Driver code
a = [3, 1, 5, 2, 4]
n = len(a)
 
farthest_min(a, n)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
class GFG {
 
    // Function to find the farthest
    // smaller number in the right side
    static void farthest_min(int[] a, int n)
    {
        // To store minimum element
        // in the range i to n
        int[] suffix_min = new int[n];
 
        suffix_min[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            suffix_min[i]
                = Math.Min(suffix_min[i + 1], a[i]);
        }
 
        for (int i = 0; i < n; i++) {
            int low = i + 1, high = n - 1, ans = -1;
 
            while (low <= high) {
                int mid = (low + high) / 2;
 
                // If currnet element in the suffix_min
                // is less than a[i] then move right
                if (suffix_min[mid] < a[i]) {
                    ans = mid;
                    low = mid + 1;
                }
                else
                    high = mid - 1;
            }
 
            // Print the required answer
            Console.Write(ans + " ");
        }
    }
 
    // Driver code
    public static void Main()
    {
        int[] a = { 3, 1, 5, 2, 4 };
        int n = a.Length;
 
        farthest_min(a, n);
    }
}
 
// This code is contributed by ihritik


输出
3 -1 4 -1 -1 

时间复杂度: O(N * log(N))
辅助空间: O(N)