📌  相关文章
📜  查找小于其右侧所有元素的所有 Array 元素

📅  最后修改于: 2022-05-13 01:56:08.534000             🧑  作者: Mango

查找小于其右侧所有元素的所有 Array 元素

给定一个包含N个正整数的数组arr[] 。任务是找到所有小于其右侧所有元素的元素。

例子:

天真的方法:这种方法使用两个循环。对于每个元素,向右遍历数组并检查是否存在任何较小的元素。如果数组右侧的所有元素都大于它,则打印该元素。

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

高效方法:在高效方法中,想法是使用堆栈。请按照以下步骤操作:

  • 从数组的开头迭代数组。
  • 对于数组中的每个元素,弹出堆栈中存在的所有大于它的元素,然后将其压入堆栈。
  • 如果没有元素大于它,那么当前元素就是答案。
  • 最后,堆栈中的元素小于其右侧的所有元素。

下面是上述方法的代码实现。

C++
// C++ program to find all elements in array
// that are smaller than all elements
// to their right.
#include 
#include 
#include 
using namespace std;
 
// Function to print all elements which are
// smaller than all elements present
// to their right
void FindDesiredElements(vector const& arr)
{
    // Create an empty stack
    stack stk;
 
    // Do for each element
    for (int i : arr) {
        // Pop all the elements that
        // are greater than the
        // current element
        while (!stk.empty() && stk.top() > i) {
            stk.pop();
        }
 
        // Push current element into the stack
        stk.push(i);
    }
 
    // Print all elements in the stack
    while (!stk.empty()) {
        cout << stk.top() << " ";
        stk.pop();
    }
}
 
// Driver Code
int main()
{
    vector arr = { 6, 14, 13, 21, 17, 19 };
 
    FindDesiredElements(arr);
    return 0;
}


Java
// Java program to find all elements in array
// that are smaller than all elements
// to their right.
import java.util.ArrayList;
import java.util.Stack;
 
class GFG {
 
  // Function to print all elements which are
  // smaller than all elements present
  // to their right
  static void FindDesiredElements(ArrayList arr)
  {
 
    // Create an empty stack
    Stack stk = new Stack();
 
    // Do for each element
    for (int i : arr)
    {
       
      // Pop all the elements that
      // are greater than the
      // current element
      while (!stk.empty() && stk.peek() > i) {
        stk.pop();
      }
 
      // Push current element into the stack
      stk.push(i);
    }
 
    // Print all elements in the stack
    while (!stk.empty()) {
      System.out.print(stk.peek() + " ");
      stk.pop();
    }
  }
 
  // Driver Code
  public static void main(String args[])
  {
    ArrayList arr = new ArrayList();
    arr.add(6);
    arr.add(14);
    arr.add(13);
    arr.add(21);
    arr.add(17);
    arr.add(19);
 
    FindDesiredElements(arr);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Oython program to find all elements in array
# that are smaller than all elements
# to their right.
 
# Function to print all elements which are
# smaller than all elements present
# to their right
def FindDesiredElements(arr):
 
    # Create an empty stack
    stk = []
 
    # Do for each element
    for i in arr :
 
        # Pop all the elements that
        # are greater than the
        # current element
        while (len(stk)!=0 and stk[len(stk)-1] > i):
            stk.pop()
 
        # Push current element into the stack
        stk.append(i)
 
    # Print all elements in the stack
    while (len(stk) != 0):
        print(stk[len(stk)-1],end = " ")
        stk.pop()
 
# Driver Code
arr = []
arr.append(6)
arr.append(14)
arr.append(13)
arr.append(21)
arr.append(17)
arr.append(19)
 
FindDesiredElements(arr)
 
# This code is contributed by shinjanpatra


C#
// C# program to find all elements in array
// that are smaller than all elements
// to their right.
using System;
using System.Collections.Generic;
public class GFG {
 
  // Function to print all elements which are
  // smaller than all elements present
  // to their right
  static void FindDesiredElements(List arr) {
 
    // Create an empty stack
    Stack stk = new Stack();
 
    // Do for each element
    foreach (int i in arr) {
 
      // Pop all the elements that
      // are greater than the
      // current element
      while (stk.Count!=0 && stk.Peek() > i) {
        stk.Pop();
      }
 
      // Push current element into the stack
      stk.Push(i);
    }
 
    // Print all elements in the stack
    while (stk.Count>0) {
      Console.Write(stk.Peek() + " ");
      stk.Pop();
    }
  }
 
  // Driver Code
  public static void Main(String []args) {
    List arr = new List();
    arr.Add(6);
    arr.Add(14);
    arr.Add(13);
    arr.Add(21);
    arr.Add(17);
    arr.Add(19);
 
    FindDesiredElements(arr);
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program to print all elements which are
// smaller than all elements present to their right
 
#include 
#include 
using namespace std;
 
// Function to print all elements which are
// smaller than all elements
// present to their right
void FindDesiredElements(int arr[], int n)
{
    int min_so_far = INT_MAX;
 
    // Traverse the array from right to left
    for (int j = n - 1; j >= 0; j--) {
        // If the current element is greater
        //  than the maximum so far, print it
        // and update `max_so_far`
        if (arr[j] <= min_so_far) {
            min_so_far = arr[j];
            cout << arr[j] << " ";
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 14, 13, 21, 17, 19 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    FindDesiredElements(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to print all elements which are
  // smaller than all elements
  // present to their right
  static void FindDesiredElements(int[] arr, int n)
  {
    int min_so_far = Integer.MAX_VALUE;
 
    // Traverse the array from right to left
    for (int j = n - 1; j >= 0; j--)
    {
 
      // If the current element is greater
      //  than the maximum so far, print it
      // and update `max_so_far`
      if (arr[j] <= min_so_far) {
        min_so_far = arr[j];
        System.out.print(arr[j] + " ");
      }
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 6, 14, 13, 21, 17, 19 };
    int N = arr.length;
 
    FindDesiredElements(arr, N);
 
  }
}
 
// This code is contributed by code_hunt.


Python3
# Python code for the above approach
 
# Function to print all elements which are
# smaller than all elements
# present to their right
def FindDesiredElements(arr, n):
    min_so_far = 10 ** 9;
 
    # Traverse the array from right to left
    for j in range(n - 1, -1, -1):
     
        # If the current element is greater
        #  than the maximum so far, print it
        # and update `max_so_far`
        if (arr[j] <= min_so_far):
            min_so_far = arr[j];
            print(arr[j], end=" ")
 
# Driver Code
arr = [6, 14, 13, 21, 17, 19];
N = len(arr)
 
FindDesiredElements(arr, N);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program to print all elements which are
// smaller than all elements present to their right
 
using System;
class GFG {
    // Function to print all elements which are
    // smaller than all elements
    // present to their right
    static void FindDesiredElements(int[] arr, int n)
    {
        int min_so_far = Int32.MaxValue;
 
        // Traverse the array from right to left
        for (int j = n - 1; j >= 0; j--) {
            // If the current element is greater
            //  than the maximum so far, print it
            // and update `max_so_far`
            if (arr[j] <= min_so_far) {
                min_so_far = arr[j];
                Console.Write(arr[j] + " ");
            }
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 6, 14, 13, 21, 17, 19 };
        int N = arr.Length;
 
        FindDesiredElements(arr, N);
    }
}


Javascript



输出
19 17 13 6 

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

空间优化方法:想法是从右到左遍历数组(倒序),并维护一个辅助变量来存储目前找到的最小元素。这种方法会忽略堆栈的使用。请按照以下步骤操作:

  • 从数组的末尾开始迭代。
  • 如果当前元素小于迄今为止的最小值,则找到该元素。更新到目前为止存储最小值的值。打印所有这些值作为答案。

下面是上述方法的实现。

C++

// C++ program to print all elements which are
// smaller than all elements present to their right
 
#include 
#include 
using namespace std;
 
// Function to print all elements which are
// smaller than all elements
// present to their right
void FindDesiredElements(int arr[], int n)
{
    int min_so_far = INT_MAX;
 
    // Traverse the array from right to left
    for (int j = n - 1; j >= 0; j--) {
        // If the current element is greater
        //  than the maximum so far, print it
        // and update `max_so_far`
        if (arr[j] <= min_so_far) {
            min_so_far = arr[j];
            cout << arr[j] << " ";
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 14, 13, 21, 17, 19 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    FindDesiredElements(arr, N);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to print all elements which are
  // smaller than all elements
  // present to their right
  static void FindDesiredElements(int[] arr, int n)
  {
    int min_so_far = Integer.MAX_VALUE;
 
    // Traverse the array from right to left
    for (int j = n - 1; j >= 0; j--)
    {
 
      // If the current element is greater
      //  than the maximum so far, print it
      // and update `max_so_far`
      if (arr[j] <= min_so_far) {
        min_so_far = arr[j];
        System.out.print(arr[j] + " ");
      }
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 6, 14, 13, 21, 17, 19 };
    int N = arr.length;
 
    FindDesiredElements(arr, N);
 
  }
}
 
// This code is contributed by code_hunt.

Python3

# Python code for the above approach
 
# Function to print all elements which are
# smaller than all elements
# present to their right
def FindDesiredElements(arr, n):
    min_so_far = 10 ** 9;
 
    # Traverse the array from right to left
    for j in range(n - 1, -1, -1):
     
        # If the current element is greater
        #  than the maximum so far, print it
        # and update `max_so_far`
        if (arr[j] <= min_so_far):
            min_so_far = arr[j];
            print(arr[j], end=" ")
 
# Driver Code
arr = [6, 14, 13, 21, 17, 19];
N = len(arr)
 
FindDesiredElements(arr, N);
 
# This code is contributed by Saurabh Jaiswal

C#

// C# program to print all elements which are
// smaller than all elements present to their right
 
using System;
class GFG {
    // Function to print all elements which are
    // smaller than all elements
    // present to their right
    static void FindDesiredElements(int[] arr, int n)
    {
        int min_so_far = Int32.MaxValue;
 
        // Traverse the array from right to left
        for (int j = n - 1; j >= 0; j--) {
            // If the current element is greater
            //  than the maximum so far, print it
            // and update `max_so_far`
            if (arr[j] <= min_so_far) {
                min_so_far = arr[j];
                Console.Write(arr[j] + " ");
            }
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 6, 14, 13, 21, 17, 19 };
        int N = arr.Length;
 
        FindDesiredElements(arr, N);
    }
}

Javascript



输出
19 17 13 6 

时间复杂度: 在)
辅助空间: O(1)