📌  相关文章
📜  通过从任何递增对中重复删除一个元素,将数组简化为单个元素

📅  最后修改于: 2021-04-22 02:07:18             🧑  作者: Mango

给定一个数组arr [] ,该数组包含范围为[1,N]的排列,任务是通过执行以下操作来检查给定的数组是否可以简化为单个非零元素:

如果可以将数组简化为单个元素,则在每个操作中打印“是” ,然后打印所选索引以及替换元素的索引。否则,打印“否”

例子:

方法:想法是先将索引[1,N – 2]中的所有元素都转换为0 ,然后在最后一步中消除第0元素或(N – 1)元素之一以获得单例数组。以下是解决问题的步骤:

  1. 选择一组可以对其执行给定操作的有效索引。
  2. 现在要选择要转换为0的元素,请检查以下条件:
    • 如果数组的第0个索引是这些索引的一部分,则将另一个索引处的元素转换为0
    • 如果第0个索引不是所选索引的一部分,则将两个元素中的较小者替换为0
  3. 继续此过程,直到单个正元素保留在数组中。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the order of
// indices of converted numbers
void order_of_removal(int a[], int n)
{
    // Stores the values of indices
    // with numbers > first index
    queue greater_indices;
 
    int first = a[0];
 
    // Stores the index of the closest
    // consecutive number to index 0
    int previous_index = 0;
 
    // Push the indices into the queue
    for (int i = 1; i < n; i++) {
        if (a[i] > first)
            greater_indices.push(i);
    }
 
    // Traverse the queue
    while (greater_indices.size() > 0) {
 
        // Stores the index of the
        // closest number > arr[0]
        int index = greater_indices.front();
 
        greater_indices.pop();
 
        int to_remove = index;
 
        // Remove elements present in
        // indices [1, to_remove - 1]
        while (--to_remove > previous_index) {
 
            cout << to_remove << " "
                 << index << " "
                 << to_remove << endl;
        }
 
        cout << 0 << " " << index << " "
             << index << endl;
 
        // Update the previous_index
        // to index
        previous_index = index;
    }
}
 
// Function to check if array arr[] can
// be reduced to single element or not
void canReduced(int arr[], int N)
{
    // If array element can't be
    // reduced to single element
    if (arr[0] > arr[N - 1]) {
        cout << "No" << endl;
    }
 
    // Otherwise find the order
    else {
        cout << "Yes" << endl;
        order_of_removal(arr, N);
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    canReduced(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to print the order of
// indices of converted numbers
public static void order_of_removal(int[] a, int n)
{
     
    // Stores the values of indices
    // with numbers > first index
    Queue greater_indices = new LinkedList<>();
   
    int first = a[0];
   
    // Stores the index of the closest
    // consecutive number to index 0
    int previous_index = 0;
   
    // Push the indices into the queue
    for(int i = 1; i < n; i++)
    {
        if (a[i] > first)
            greater_indices.add(i);
    }
   
    // Traverse the queue
    while (greater_indices.size() > 0)
    {
         
        // Stores the index of the
        // closest number > arr[0]
        int index = greater_indices.peek();
   
        greater_indices.remove();
   
        int to_remove = index;
   
        // Remove elements present in
        // indices [1, to_remove - 1]
        while (--to_remove > previous_index)
        {
            System.out.println(to_remove + " " +
                                   index + " " +
                                   to_remove);
        }
   
        System.out.println(0 + " " + index +
                               " " + index);
   
        // Update the previous_index
        // to index
        previous_index = index;
    }
}
   
// Function to check if array arr[] can
// be reduced to single element or not
public static void canReduced(int[] arr, int N)
{
     
    // If array element can't be
    // reduced to single element
    if (arr[0] > arr[N - 1])
    {
        System.out.println("No");
    }
   
    // Otherwise find the order
    else
    {
        System.out.println("Yes");
        order_of_removal(arr, N);
    }
} 
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4 };
   
    int N = arr.length;
   
    // Function call
    canReduced(arr, N);
}
}
 
// This code is contributed divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to print the order of
# indices of converted numbers
def order_of_removal(a, n) :
 
    # Stores the values of indices
    # with numbers > first index
    greater_indices = []
 
    first = a[0]
 
    # Stores the index of the closest
    # consecutive number to index 0
    previous_index = 0
 
    # Push the indices into the queue
    for i in range(1, n) :
        if (a[i] > first) :
            greater_indices.append(i)
 
    # Traverse the queue
    while (len(greater_indices) > 0) :
 
        # Stores the index of the
        # closest number > arr[0]
        index = greater_indices[0];
 
        greater_indices.pop(0)
 
        to_remove = index
 
        # Remove elements present in
        # indices [1, to_remove - 1]
        to_remove =- 1
        while (to_remove > previous_index) :
 
            print(to_remove, index, to_remove)
 
        print(0, index, index)
 
        # Update the previous_index
        # to index
        previous_index = index
 
# Function to check if array arr[] can
# be reduced to single element or not
def canReduced(arr, N) :
 
    # If array element can't be
    # reduced to single element
    if (arr[0] > arr[N - 1]) :
        print("No")
 
    # Otherwise find the order
    else :
        print("Yes")
        order_of_removal(arr, N)
 
# Given array arr[]
arr = [ 1, 2, 3, 4 ]
 
N = len(arr)
 
# Function Call
canReduced(arr, N)
 
# This code is contributed by divyesh072019


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function to print the order of
// indices of converted numbers
public static void order_of_removal(int[] a,
                                    int n)
{
  // Stores the values of indices
  // with numbers > first index
  Queue greater_indices = new Queue();
 
  int first = a[0];
 
  // Stores the index of the closest
  // consecutive number to index 0
  int previous_index = 0;
 
  // Push the indices into the queue
  for(int i = 1; i < n; i++)
  {
    if (a[i] > first)
      greater_indices.Enqueue(i);
  }
 
  // Traverse the queue
  while (greater_indices.Count > 0)
  {
    // Stores the index of the
    // closest number > arr[0]
    int index = greater_indices.Peek();
 
    greater_indices.Dequeue();
 
    int to_remove = index;
 
    // Remove elements present in
    // indices [1, to_remove - 1]
    while (--to_remove > previous_index)
    {
      Console.WriteLine(to_remove + " " +
                        index + " " + to_remove);
    }
 
    Console.WriteLine(0 + " " + index +
                      " " + index);
 
    // Update the previous_index
    // to index
    previous_index = index;
  }
}
 
// Function to check if array []arr can
// be reduced to single element or not
public static void canReduced(int[] arr,
                              int N)
{
  // If array element can't be
  // reduced to single element
  if (arr[0] > arr[N - 1])
  {
    Console.WriteLine("No");
  }
 
  // Otherwise find the order
  else
  {
    Console.WriteLine("Yes");
    order_of_removal(arr, N);
  }
} 
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {1, 2, 3, 4};
 
  int N = arr.Length;
 
  // Function call
  canReduced(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


输出:
Yes
0 1 1
0 2 2
0 3 3

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