给定一个由N 个不同整数组成的数组arr[] ,任务是为每个数组元素打印其左侧存在的所有较大元素。
例子:
Input: arr[] = {5, 3, 9, 0, 16, 12}
Output:
5:
3: 5
9:
0: 9 5 3
16:
12: 16
Input: arr[] = {1, 2, 0}
Output:
1:
2:
0: 2 1
朴素的方法:解决问题的最简单的方法是遍历数组,对于每个数组元素,遍历其前面的所有元素并打印大于当前元素的元素。
C++
// C++ program for Naive approach
#include
using namespace std;
// Function to print all greater elements on the left of each array element
void printGreater(vector& arr)
{
// store the size of array in varaible n
int n = arr.size();
for (int i = 0; i < n; i++)
{
// result is used to store elements which are greater than current element present left side of current element
vector result;
// traversing all the elements which are left of current element arr[i]
for (int j = i - 1; j >= 0; j--)
{
//checking whether arr[j] (left element) is greater than current element or not
// if yes then insert the element to the result vector
if (arr[j] > arr[i])
{
result.push_back(arr[j]);
}
}
cout << arr[i] << ": ";
//printing all the elements present in result vector
for (int k = 0; k < result.size(); k++)
{
cout << result[k] << " ";
}
cout << endl;
}
}
//Driver Code
int main()
{
vector arr{5, 3, 9, 0, 16, 12};
printGreater(arr);
return 0;
}
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to print all greater elements
// on the left of each array element
void printGreater(vector& arr)
{
int n = arr.size();
// Set to implement
// self-balancing BSTs
set > s;
// Traverse the array
for (int i = 0; i < n; i++) {
// Insert the current
// element into the set
auto p = s.insert(arr[i]);
auto j = s.begin();
cout << arr[i] << ": ";
// Iterate through the set
while (j != p.first) {
// Print the element
cout << *j << " ";
j++;
}
cout << endl;
}
}
// Driver Code
int main()
{
vector arr{ 5, 3, 9, 0, 16, 12 };
printGreater(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to print all greater elements
// on the left of each array element
static void printGreater(int arr[])
{
int n = arr.length;
// Set to implement
// self-balancing BSTs
TreeSet s = new TreeSet<>(
Collections.reverseOrder());
// Traverse the array
for(int i = 0; i < n; i++)
{
// Insert the current
// element into the set
s.add(arr[i]);
System.out.print(arr[i] + ": ");
// Iterate through the set
for(int v : s)
{
if (v == arr[i])
break;
// Print the element
System.out.print(v + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 3, 9, 0, 16, 12 };
printGreater(arr);
}
}
// This code is contributed by Kingash
输出
5:
3: 5
9:
0: 9 3 5
16:
12: 16
时间复杂度: O(N 2 )
辅助空间: O(N)
高效方法:为了优化上述方法,其想法是利用自平衡二叉搜索树。 C++中的Set是使用自平衡BSTs实现的,可以用来解决这个问题。请按照以下步骤解决问题:
- 初始化一个 Set s ,以非递减顺序存储元素。
- 在索引0到N – 1 上遍历数组并执行以下操作:
- 将arr[i]插入到集合s 中。
- 从 Set 的开头迭代到p并打印 Set 中的元素。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to print all greater elements
// on the left of each array element
void printGreater(vector& arr)
{
int n = arr.size();
// Set to implement
// self-balancing BSTs
set > s;
// Traverse the array
for (int i = 0; i < n; i++) {
// Insert the current
// element into the set
auto p = s.insert(arr[i]);
auto j = s.begin();
cout << arr[i] << ": ";
// Iterate through the set
while (j != p.first) {
// Print the element
cout << *j << " ";
j++;
}
cout << endl;
}
}
// Driver Code
int main()
{
vector arr{ 5, 3, 9, 0, 16, 12 };
printGreater(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to print all greater elements
// on the left of each array element
static void printGreater(int arr[])
{
int n = arr.length;
// Set to implement
// self-balancing BSTs
TreeSet s = new TreeSet<>(
Collections.reverseOrder());
// Traverse the array
for(int i = 0; i < n; i++)
{
// Insert the current
// element into the set
s.add(arr[i]);
System.out.print(arr[i] + ": ");
// Iterate through the set
for(int v : s)
{
if (v == arr[i])
break;
// Print the element
System.out.print(v + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 3, 9, 0, 16, 12 };
printGreater(arr);
}
}
// This code is contributed by Kingash
输出
5:
3: 5
9:
0: 9 5 3
16:
12: 16
时间复杂度:O(N 2 )
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live