📜  使用 a[i+1] > a[i] 最大化元素

📅  最后修改于: 2021-10-25 11:17:59             🧑  作者: Mango

给定一个包含 N 个整数的数组,重新排列数组元素,使得下一个数组元素大于前一个元素 ( A_{(i+1)}  > A_i  )。
例子:

如果所有元素都是不同的,那么答案就是 n-1,其中 n 是数组中的元素数。如果有重复元素,则答案为 n – max_freq。

C++
#include
using namespace std;
 
// returns the number of positions where A(i + 1) is
// greater than A(i) after rearrangement of the array
int countMaxPos(int arr[], int n)
{
 
    // Creating a HashMap containing char
    // as a key and occurrences as a value
    unordered_map map;
     
    for (int i = 0; i < n; i++ ) {
        if (map.count(arr[i]))
            map.insert({arr[i], (map.count(arr[i]) + 1)});
        else
            map.insert({arr[i], 1});
    }
     
    // Find the maximum frequency
    int max_freq = 0;
 
    for (auto i : map) {
        if (max_freq < i.second)
        {
            max_freq = i.second;
        }
    }
    return n - max_freq;
}
 
// Driver code
int main()
{
    int arr[] = { 20, 30, 10, 50, 40 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << (countMaxPos(arr, n));
}
 
// This code is contributed by Rajput-Ji


Java
import java.util.*;
 
class GFG {
 
    // returns the number of positions where A(i + 1) is
    // greater than A(i) after rearrangement of the array
    static int countMaxPos(int[] arr)
    {
        int n = arr.length;
 
        // Creating a HashMap containing char
        // as a key and occurrences as  a value
        HashMap map
            = new HashMap();
        for (int x : arr) {
            if (map.containsKey(x))
                map.put(x, map.get(x) + 1);
            else
                map.put(x, 1);
        }
 
        // Find the maximum frequency
        int max_freq = 0;
        for (Map.Entry entry : map.entrySet())
            max_freq = Math.max(max_freq, (int)entry.getValue());
 
        return n - max_freq;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 20, 30, 10, 50, 40 };
        System.out.println(countMaxPos(arr));
    }
}


Python3
# Python3 implementation of the above approach
 
# Returns the number of positions where
# A(i + 1) is greater than A(i) after
# rearrangement of the array
def countMaxPos(arr):
     
    n = len(arr)
 
    # Creating a HashMap containing char
    # as a key and occurrences as a value
    Map = {}
    for x in arr:
        if x in Map:
            Map[x] += 1
        else:
            Map[x] = 1
         
    # Find the maximum frequency
    max_freq = 0
    for entry in Map:
        max_freq = max(max_freq, Map[entry])
 
    return n - max_freq
 
# Driver code
if __name__ == "__main__":
     
    arr = [20, 30, 10, 50, 40]
    print(countMaxPos(arr))
     
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;            
 
class GFG
{
 
    // returns the number of positions where
    // A(i + 1) is greater than A(i) after
    // rearrangement of the array
    static int countMaxPos(int[] arr)
    {
        int n = arr.Length;
 
        // Creating a HashMap containing char
        // as a key and occurrences as a value
        Dictionary map = new Dictionary();
        foreach (int x in arr)
        {
            if (map.ContainsKey(x))
                map[x] = map[x] + 1;
            else
                map.Add(x, 1);
        }
 
        // Find the maximum frequency
        int max_freq = 0;
        foreach(KeyValuePair entry in map)
            max_freq = Math.Max(max_freq, entry.Value);
 
        return n - max_freq;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] arr = { 20, 30, 10, 50, 40 };
        Console.WriteLine(countMaxPos(arr));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程