原文:

给定一个由 n 个不同整数组成的 数组【arr】,任务是为每个数组元素打印其左侧所有个更大的元素

示例:

输入: arr[] = {5,3,9,0,16,12} 输出: 5: 3:5 9: 0:9 5 3 16: 12:16

输入: arr[] = {1,2,0} 输出: 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 variable 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;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for naive approach
import java.util.*;
class gfg{
  // function to print all greater elements on the left of each array element
  static void printgreater(arraylist arr)
  {
    // store the size of array in variable 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
      arraylist result
        = new arraylist();
      // 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.get(j) > arr.get(i))
        {
          result.add(arr.get(j));
        }
      }
      system.out.print(arr.get(i)   ": ");
      // printing all the elements present in result vector
      for (int k = 0; k < result.size(); k  )
      {
        system.out.print(result.get(k)  " ");
      }
      system.out.println();
    }
  }
  // driver code
  public static void main(string args[])
  {
    arraylist arr = new arraylist(arrays.aslist(5, 3, 9, 0, 16, 12));
    printgreater(arr);
  }
}
// this code is contributed by bgangwar59.

python 3

# python3 program for naive approach
# function to print all greater
# elements on the left of each
# array element
def printgreater(arr):
    # store the size of array
    # in variable n
    n = len(arr)
    for i in range(n):
        # result is used to store elements
        # which are greater than current
        # element present left side of
        # current element
        result = []
        # traversing all the elements
        # which are left of current
        # element arr[i]
        j = i - 1
        while(j >= 0):
            # 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.append(arr[j])
            j -= 1
        print(arr[i], end = ": ")
        # printing all the elements present
        # in result vector
        for k in range(len(result)):
            print(result[k], end = " ")
        print("\n", end = "")
# driver code
if __name__ == '__main__':
    arr = [ 5, 3, 9, 0, 16, 12 ]
    printgreater(arr)
# this code is contributed by ipg2016107

c

// c# program for naive approach
using system;
using system.collections.generic;
class gfg
{
    // function to print all greater elements on the left of
    // each array element
    static void printgreater(int[] arr)
    {
        // store the size of array in variable n
        int n = arr.length;
        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
            list result = new list();
            // 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.add(arr[j]);
                }
            }
            console.write(arr[i]   ": ");
            // printing all the elements present in result
            // vector
            for (int k = 0; k < result.count; k  ) {
                console.write(result[k]   " ");
            }
            console.writeline();
        }
    }
    // driver code
    public static void main()
    {
        int[] arr = { 5, 3, 9, 0, 16, 12 };
        printgreater(arr);
    }
}
// this code is contributed by ukasp.

java 描述语言


output

5: 
3: 5 
9: 
0: 9 3 5 
16: 
12: 16 

时间复杂度:o(n2) t5】辅助空间: o(n)

高效方法:为了优化上述方法,想法是利用一个。是使用自平衡 bst 实现的,可以用来解决这个问题。按照以下步骤解决问题:

  • 初始化一个和,以非递减顺序存储元素。
  • 在索引 0n–1上遍历数组,并执行以下操作:
    • t4 中。
    • 开始迭代到 p 并打印集合中的元素。

以下是上述方法的实现:

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

python 3

# python3 program for the above approach
# function to print all greater elements
# on the left of each array element
def printgreater(arr):
    n = len(arr)
    # set to implement
    # self-balancing bsts
    s = set([])
    # traverse the array
    for i in range(n):
        # insert the current
        # element into the set
        s.add(arr[i])
        print(arr[i], ": ", sep = "", end = "")
        temp = list(s)
        temp.sort()
        temp.reverse()
        # iterate through the set
        for v in range(len(temp)):
            if (temp[v] == arr[i]):
                break
            # print the element
            print(temp[v], end = " ")
        print()
arr = [5, 3, 9, 0, 16, 12]
printgreater(arr)
# this code is contributed by divyesh072019.

c

// c# program for the above approach
using system;
using system.collections.generic;
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
        hashset s = new hashset();
        // traverse the array
        for(int i = 0; i < n; i  )
        {
            // insert the current
            // element into the set
            s.add(arr[i]);
            console.write(arr[i]   ": ");
            list temp = new list();
            // iterate through the set
            foreach(int v in s)
            {
                temp.add(v);
            }
            temp.sort();
            temp.reverse();
            // iterate through the set
            for(int v = 0; v < temp.count; v  )
            {
                if (temp[v] == arr[i])
                {
                    break;
                }
                // print the element
                console.write(temp[v]   " ");
            }
            console.writeline();
        }
    }
  // driver code
  static void main() {
    int[] arr = { 5, 3, 9, 0, 16, 12 };
    printgreater(arr);
  }
}
// this code is contributed by rameshtravel07.

java 描述语言


output

5: 
3: 5 
9: 
0: 9 5 3 
16: 
12: 16 

时间复杂度:o(n2) 辅助空间: o(n)