📌  相关文章
📜  通过删除对并用它们的平均值替换它们来最小化剩余的数组元素

📅  最后修改于: 2021-10-26 05:13:19             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是通过从数组中重复删除一对(例如(arr[i], arr[j])并插入它们的平均值的 Ceil 值来找到可能的最小剩余数组元素.

例子:

方法:该问题可以使用贪心技术解决。这个想法是重复删除第一大和第二大数组元素并插入它们的平均值。最后,打印数组中剩下的最小元素。

  • 初始化一个priority_queue,比如PQ ,来存储数组元素,这样最大的元素总是出现在PQ的顶部。
  • 遍历数组并将所有数组元素存储在PQ 中
  • 遍历priority_queue的元素,而在priority_queue元件的计数大于1。在每次迭代中,从PQ 中弹出前两个元素并插入它们平均值的Ceil 值
  • 最后,打印PQ 中剩下的元素。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the smallest element
// left in the array by removing pairs
// and inserting their average
int findSmallestNumLeft(int arr[], int N)
{
    // Stores array elements such that the
    // largest element present at top of PQ
    priority_queue PQ;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Insert arr[i] into PQ
        PQ.push(arr[i]);
    }
 
    // Iterate over elements of PQ while count
    // of elements in PQ greater than 1
    while (PQ.size() > 1) {
 
        // Stores largest element of PQ
        int top1 = PQ.top();
 
        // Pop the largest element of PQ
        PQ.pop();
 
        // Stores largest element of PQ
        int top2 = PQ.top();
 
        // Pop the largest element of PQ
        PQ.pop();
 
        // Insert the ceil value of average
        // of top1 and top2
        PQ.push((top1 + top2 + 1) / 2);
    }
 
    return PQ.top();
}
 
// Driver Code
int main()
{
    int arr[] = { 30, 16, 40 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << findSmallestNumLeft(
        arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.PriorityQueue;
 
class GFG{
 
// Function to find the smallest element
// left in the array by removing pairs
// and inserting their average
static int findSmallestNumLeft(int arr[], int N)
{
   
    // Stores array elements such that the
    // largest element present at top of PQ
    PriorityQueue PQ = new PriorityQueue((a,b)->b-a);
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Insert arr[i] into PQ
        PQ.add(arr[i]);
    }
 
    // Iterate over elements of PQ while count
    // of elements in PQ greater than 1
    while (PQ.size() > 1)
    {
 
        // Stores largest element of PQ
        int top1 = PQ.peek();
 
        // Pop the largest element of PQ
        PQ.remove();
 
        // Stores largest element of PQ
        int top2 = PQ.peek();
 
        // Pop the largest element of PQ
        PQ.remove();
 
        // Insert the ceil value of average
        // of top1 and top2
        PQ.add((top1 + top2 + 1) / 2);
    }
 
    return PQ.peek();
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 30, 16, 40 };
    int N = arr.length;
 
    System.out.print(findSmallestNumLeft(
        arr, N));
 
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the smallest element
# left in the array by removing pairs
# and inserting their average
def findSmallestNumLeft(arr, N):
     
    # Stores array elements such that the
    # largest element present at top of PQ
    PQ = []
 
    # Traverse the array
    for i in range(N):
         
        # Insert arr[i] into PQ
        PQ.append(arr[i])
 
    # Iterate over elements of PQ while count
    # of elements in PQ greater than 1
    PQ = sorted(PQ)
 
    while (len(PQ) > 1):
 
        # Stores largest element of PQ
        top1 = PQ[-1]
 
        # Pop the largest element of PQ
        del PQ[-1]
 
        # Stores largest element of PQ
        top2 = PQ[-1]
 
        # Pop the largest element of PQ
        del PQ[-1]
 
        # Insert the ceil value of average
        # of top1 and top2
        PQ.append((top1 + top2 + 1) // 2)
        PQ = sorted(PQ)
 
    return PQ[-1]
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 30, 16, 40 ]
    N = len(arr)
 
    print (findSmallestNumLeft(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GfG
{
    // Function to find the smallest element
    // left in the array by removing pairs
    // and inserting their average
    static int findSmallestNumLeft(int[] arr, int N)
    {
        // Stores array elements such that the
        // largest element present at top of PQ
        List PQ = new List();
      
        // Traverse the array
        for (int i = 0; i < N; i++) {
      
            // Insert arr[i] into PQ
            PQ.Add(arr[i]);
        }
         
        PQ.Sort();
        PQ.Reverse();
         
        // Iterate over elements of PQ while count
        // of elements in PQ greater than 1
        while (PQ.Count > 1) {
      
            // Stores largest element of PQ
            int top1 = PQ[0];
      
            // Pop the largest element of PQ
            PQ.RemoveAt(0);
      
            // Stores largest element of PQ
            int top2 = PQ[0];
      
            // Pop the largest element of PQ
            PQ.RemoveAt(0);
      
            // Insert the ceil value of average
            // of top1 and top2
            PQ.Add((top1 + top2 + 1) / 2);
             
            PQ.Sort();
            PQ.Reverse();
        }
      
        return PQ[0];
    }
 
  // Driver code
    public static void Main()
    {
        int[] arr = { 30, 16, 40 };
        int N = arr.Length;
      
        Console.Write(findSmallestNumLeft(arr, N));
    }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
26

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

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