给定一个数组arr [] ,该数组包含范围为[1,N]的排列,任务是通过执行以下操作来检查给定的数组是否可以简化为单个非零元素:
Select indices i and j such that i < j and arr[i] < arr[j] and convert one of the two elements to 0.
如果可以将数组简化为单个元素,则在每个操作中打印“是” ,然后打印所选索引以及替换元素的索引。否则,打印“否” 。
例子:
Input: arr[] = {2, 4, 6, 1, 3, 5}
Output:
Yes
0 1 1
0 2 2
3 4 3
0 4 4
0 5 5
Explanation:
In the first operation choose the elements 2 and 4 at indices 0 and 1. Convert the element 4 at index 1 to 0, arr[] = {2, 0, 6, 1, 3, 5}
In the second operation choose the elements 2 and 6 at indices 0 and 2. Convert the element 6 at index 2 to 0, arr[] = {2, 0, 0, 1, 3, 5}
In the third operation choose the elements 1 and 3 at indices 3 and 4. Convert the element 1 at index 3 to 0, arr[] = {2, 0, 0, 0, 3, 5}
In the forth operation choose the elements 2 and 3 at indices 0 and 4. Convert the element 3 at index 4 to 0, arr[] = {2, 0, 0, 0, 0, 5}
In the fifth operation choose the elements 2 and 5 at indices 0 and 5. Convert the element 5 at index 5 to 0, arr[] = {2, 0, 0, 0, 0, 0}
So, the array is reduced to a single positive element in 5 operations.
Input: arr[] = {2, 3, 1}
Output: No
Explanation:
There is not set of operations in which the given array can be converted to a single element.
方法:想法是先将索引[1,N – 2]中的所有元素都转换为0 ,然后在最后一步中消除第0个元素或第(N – 1)个元素之一以获得单例数组。以下是解决问题的步骤:
- 选择一组可以对其执行给定操作的有效索引。
- 现在要选择要转换为0的元素,请检查以下条件:
- 如果数组的第0个索引是这些索引的一部分,则将另一个索引处的元素转换为0 。
- 如果第0个索引不是所选索引的一部分,则将两个元素中的较小者替换为0 。
- 继续此过程,直到单个正元素保留在数组中。
下面是上述方法的实现:
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)