📌  相关文章
📜  通过给定的操作将数组减少到最多一个元素

📅  最后修改于: 2021-09-03 04:05:24             🧑  作者: Mango

给定一个整数数组arr[] ,任务是在执行以下操作后找到数组中的剩余元素:

  • 在每一轮中,从数组中选择两个最大的整数 X 和 Y。
  • 如果 X == Y,则从数组中删除两个元素。
  • 如果 X != Y,则在数组中插入一个等于abs(X – Y)的元素。

注意:如果没有元素剩余,则打印 0。
例子:

方法:
为了解决上面提到的问题,我们需要使用堆数据结构。使用堆是因为我们只需要每个时刻的当前最大元素,而不关心其余元素。

  • 从给定数组的元素创建一个最大堆。
  • 每次迭代都提取顶部元素两次。如果它们的绝对差不为零,则将它们的差回队列。
  • 继续直到只剩下一个元素或没有元素。
  • 如果没有元素剩余,则打印 0。否则,打印剩余的元素。

下面是上述方法的实现:

C++
// C++ Program to reduce the
// array to almost one element
// by the given operations
  
#include 
using namespace std;
  
// Function to find the remaining
// element of array
int reduceArray(vector& arr)
{
    priority_queue p;
  
    // Build a priority queue
    // using the array
    for (int i = 0; i < arr.size(); ++i)
        p.push(arr[i]);
  
    // Continue until the priority
    // queue has one or no elements
    // remaining
    while (p.size() > 1) {
  
        // Top-most integer from heap
        int y = p.top();
        p.pop();
  
        // Second integer from heap
        int x = p.top();
        p.pop();
  
        // Resultant value
        int val = y - x;
        if (val != 0)
            p.push(val);
    }
  
    // Return 0 if no elements are left
    if (p.size() == 0)
        return 0;
  
    // Return top value of the heap
    return p.top();
}
  
// Driver code
int main()
{
  
    vector arr
        = { 3, 4, 6, 2, 7, 1 };
    cout << reduceArray(arr)
         << "\n";
    return 0;
}


Java
// Java program to reduce the
// array to almost one element
// by the given operations
import java.util.*;
  
class GFG{
  
// Function to find the remaining
// element of array
static int reduceArray(int[] arr)
{
    PriorityQueue p = new PriorityQueue<>(
        11, Collections.reverseOrder());
  
    // Build a priority queue
    // using the array
    for(int i = 0; i < arr.length; ++i)
        p.add(arr[i]);
          
    // Continue until the priority
    // queue has one or no elements
    // remaining
    while (p.size() > 1)
    {
  
        // Top-most integer from heap
        int y = p.poll();
  
        // Second integer from heap
        int x = p.poll();
          
        // Resultant value
        int val = y - x;
        if (val != 0)
            p.add(val);
    }
      
    // Return 0 if no elements are left
    if (p.size() == 0)
        return 0;
  
    // Return top value of the heap
    return p.poll();
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 4, 6, 2, 7, 1 };
      
    System.out.println(reduceArray(arr));
}
}
  
// This code is contributed by jrishabh99


Python3
# Python3 program to reduce the
# array to almost one element
# by the given operations
  
# Function to find the remaining
# element of array
def reduceArray(arr):
      
    p = []
  
    # Build a priority queue
    # using the array
    for i in range(len(arr)):
        p.append(arr[i])
    p.sort(reverse = True)
  
    # Continue until the priority
    # queue has one or no elements
    # remaining
    while (len(p) > 1):
          
        # Top-most integer from heap
        y = p[0]
        p = p[1:]
  
        # Second integer from heap
        x = p[0]
        p = p[1:]
  
        # Resultant value
        val = y - x
          
        if (val != 0):
            p.append(val)
        p.sort(reverse = True)
  
    # Return 0 if no elements are left
    if (len(p) == 0):
        return 0
  
    # Return top value of the heap
    return p[0]
  
# Driver code
if __name__ == '__main__':
      
    arr = [ 3, 4, 6, 2, 7, 1 ]
      
    print(reduceArray(arr))
  
# This code is contributed by Surendra_Gangwar


输出:
1

时间复杂度: O(N)

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