📌  相关文章
📜  生成一个数组,由每个数组元素右侧出现的最频繁的较大元素组成

📅  最后修改于: 2021-09-07 05:19:02             🧑  作者: Mango

给定大小为N的数组A[] ,任务是根据以下条件生成数组B[]

  • 对于每个数组元素A[i] ,找到出现在A[i]右侧的大于A[i]的最频繁元素将该元素插入B[]
  • 如果右侧存在多个这样的元素,则选择具有最小值的元素。
  • 如果没有元素大于A [i]为存在于A [I]的右侧, 然后将-1插入B[]

最后,打印得到的数组B[]

例子:

朴素的方法:按照以下步骤解决问题:

  • 初始化一个数组,比如V,以存储结果数组元素。
  • 使用变量遍历数组A[] ,例如i ,并执行以下操作:
    • 将变量ans初始化为-1 ,将freq初始化为0,以存储当前索引及其频率的结果。
    • 使用变量j在范围[i+1, N-1] 上迭代,并执行以下操作:
      • 如果A[j] ≤ A[i]的频率,则继续。
      • 否则,检查A[j]的频率是否> freq 。如果发现为真,则更新ANSA [j]频率A [j]的频率。
      • 否则,如果A[j] 的频率等于freq ,则将ans更新为A[j]ans之间的较小值。
    • ans的值插入数组V
  • 打印数组, V作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to generate an array containing
// the most frequent greater element on the
// right side of each array element
void findArray(int arr[], int n)
{
    // Stores the generated array
    vector v;
 
    // Traverse the array arr[]
    for (int i = 0; i < n; i++) {
 
        // Store the result for the
        // current index and its frequency
        int ans = -1, old_c = 0;
 
        // Iterate over the right subarray
        for (int j = i + 1; j < n; j++) {
 
            if (arr[j] > arr[i]) {
 
                // Store the frequency of
                // the current array element
                int curr_c
                    = count(&arr[j], &arr[n], arr[j]);
 
                // If the frequncies are equal
                if (curr_c == old_c) {
 
                    // Update ans to smaller
                    // of the two elements
                    if (arr[j] < ans)
                        ans = arr[j];
                }
 
                // If count of new element
                // is more than count of ans
                if (curr_c > old_c) {
                    ans = arr[j];
                    old_c = curr_c;
                }
            }
        }
 
        // Insert answer in the array
        v.push_back(ans);
    }
 
    // Print the resultant array
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { 4, 5, 2, 25, 10, 5,
                  10, 3, 10, 5 };
    int size = sizeof(arr)
               / sizeof(arr[0]);
 
    findArray(arr, size);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to generate an array containing
// the most frequent greater element on the
// right side of each array element
static void findArray(int arr[], int n)
{
     
    // Stores the generated array
    Vector v = new Vector();
     
    // Traverse the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Store the result for the
        // current index and its frequency
        int ans = -1, old_c = 0;
         
        // Iterate over the right subarray
        for(int j = i + 1; j < n; j++)
        {
            if (arr[j] > arr[i])
            {
                 
                // Store the frequency of
                // the current array element
                int curr_c = 0;
                for(int k = j; k < n; k++)
                {
                    if (arr[k] == arr[j])
                    {
                        curr_c++;
                    }
                };
                 
                // If the frequncies are equal
                if (curr_c == old_c)
                {
                     
                    // Update ans to smaller
                    // of the two elements
                    if (arr[j] < ans)
                        ans = arr[j];
                }
                 
                // If count of new element
                // is more than count of ans
                if (curr_c > old_c)
                {
                    ans = arr[j];
                    old_c = curr_c;
                }
            }
        }
         
        // Insert answer in the array
        v.add(ans);
    }
     
    // Print the resultant array
    for(int i = 0; i < v.size(); i++)
        System.out.print(v.get(i) + " ");
}
 
// Driver Code
public static void main (String[] args)
{
 
    // Given Input
    int arr[] = { 4, 5, 2, 25, 10,
                  5, 10, 3, 10, 5 };
    int size = arr.length;
     
    findArray(arr, size);
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python3 program for the above approach
 
# Function to generate an array containing
# the most frequent greater element on the
# right side of each array element
def findArray(arr, n):
     
    # Stores the generated array
    v = []
 
    # Traverse the array arr[]
    for i in range(n):
         
        # Store the result for the
        # current index and its frequency
        ans = -1
        old_c = 0
 
        # Iterate over the right subarray
        for j in range(i + 1, n):
            if (arr[j] > arr[i]):
 
                # Store the frequency of
                # the current array element
                curr_c = arr[j : n + 1].count(arr[j])
 
                # If the frequncies are equal
                if (curr_c == old_c):
 
                    # Update ans to smaller
                    # of the two elements
                    if (arr[j] < ans):
                        ans = arr[j]
 
                # If count of new element
                # is more than count of ans
                if (curr_c > old_c):
                    ans = arr[j]
                    old_c = curr_c
 
        # Insert answer in the array
        v.append(ans)
 
    # Print the resultant array
    for i in range(len(v)):
        print(v[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    arr = [ 4, 5, 2, 25, 10,
            5, 10, 3, 10, 5 ]
    size = len(arr)
 
    findArray(arr, size)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to generate an array containing
// the most frequent greater element on the
// right side of each array element
static void findArray(int[] arr, int n)
{
     
    // Stores the generated array
    List v = new List();
     
    // Traverse the array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Store the result for the
        // current index and its frequency
        int ans = -1, old_c = 0;
         
        // Iterate over the right subarray
        for(int j = i + 1; j < n; j++)
        {
            if (arr[j] > arr[i])
            {
                 
                // Store the frequency of
                // the current array element
                int curr_c = 0;
                for(int k = j; k < n; k++)
                {
                    if (arr[k] == arr[j])
                    {
                        curr_c++;
                    }
                };
                 
                // If the frequncies are equal
                if (curr_c == old_c)
                {
                     
                    // Update ans to smaller
                    // of the two elements
                    if (arr[j] < ans)
                        ans = arr[j];
                }
                 
                // If count of new element
                // is more than count of ans
                if (curr_c > old_c)
                {
                    ans = arr[j];
                    old_c = curr_c;
                }
            }
        }
         
        // Insert answer in the array
        v.Add(ans);
    }
     
    // Print the resultant array
    for(int i = 0; i < v.Count; i++)
        Console.Write(v[i] + " ");
}
 
// Driver Code
public static void Main()
{
     
    // Given Input
    int[] arr= { 4, 5, 2, 25, 10,
                 5, 10, 3, 10, 5 };
    int size = arr.Length;
     
    findArray(arr, size);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
5 10 10 -1 -1 10 -1 5 -1 -1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live