Given an array arr[] consisting of permutation in the range [1, N], the task is to check if the given array can be reduced to a single non-zero element by performing the following operations:
Select indices i and j such that i < j and arr[i] < arr[j] and convert one of the two elements to 0.
If it is possible to reduce the array to a single element, then print “Yes” followed by the chosen indices along with the index of the replaced element in each operation. Otherwise, print “No”.
Examples:
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.
Approach: The idea is to convert all elements from indices [1, N – 2] first to 0 and then eliminate one of either the 0th or (N – 1)th element in the last move to obtain the singleton array. Below are the steps to solve the problem:
- Choose a valid set of indices on which the given operation can be performed.
- Now to choose which element to be converted to 0 check the following conditions:
- If the 0th index of the array is a part of these indices, then convert the element at the other index to 0.
- If 0th index is not a part of the chosen indices, then replace the smaller of the two elements to 0.
- Continue this process until a single positive element remains in the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void order_of_removal( int a[], int n)
{
queue< int > greater_indices;
int first = a[0];
int previous_index = 0;
for ( int i = 1; i < n; i++) {
if (a[i] > first)
greater_indices.push(i);
}
while (greater_indices.size() > 0) {
int index = greater_indices.front();
greater_indices.pop();
int to_remove = index;
while (--to_remove > previous_index) {
cout << to_remove << " "
<< index << " "
<< to_remove << endl;
}
cout << 0 << " " << index << " "
<< index << endl;
previous_index = index;
}
}
void canReduced( int arr[], int N)
{
if (arr[0] > arr[N - 1]) {
cout << "No" << endl;
}
else {
cout << "Yes" << endl;
order_of_removal(arr, N);
}
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
canReduced(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void order_of_removal( int [] a, int n)
{
Queue<Integer> greater_indices = new LinkedList<>();
int first = a[ 0 ];
int previous_index = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (a[i] > first)
greater_indices.add(i);
}
while (greater_indices.size() > 0 )
{
int index = greater_indices.peek();
greater_indices.remove();
int to_remove = index;
while (--to_remove > previous_index)
{
System.out.println(to_remove + " " +
index + " " +
to_remove);
}
System.out.println( 0 + " " + index +
" " + index);
previous_index = index;
}
}
public static void canReduced( int [] arr, int N)
{
if (arr[ 0 ] > arr[N - 1 ])
{
System.out.println( "No" );
}
else
{
System.out.println( "Yes" );
order_of_removal(arr, N);
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
canReduced(arr, N);
}
}
|
Python3
def order_of_removal(a, n) :
greater_indices = []
first = a[ 0 ]
previous_index = 0
for i in range ( 1 , n) :
if (a[i] > first) :
greater_indices.append(i)
while ( len (greater_indices) > 0 ) :
index = greater_indices[ 0 ];
greater_indices.pop( 0 )
to_remove = index
to_remove = - 1
while (to_remove > previous_index) :
print (to_remove, index, to_remove)
print ( 0 , index, index)
previous_index = index
def canReduced(arr, N) :
if (arr[ 0 ] > arr[N - 1 ]) :
print ( "No" )
else :
print ( "Yes" )
order_of_removal(arr, N)
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
canReduced(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void order_of_removal( int [] a,
int n)
{
Queue< int > greater_indices = new Queue< int >();
int first = a[0];
int previous_index = 0;
for ( int i = 1; i < n; i++)
{
if (a[i] > first)
greater_indices.Enqueue(i);
}
while (greater_indices.Count > 0)
{
int index = greater_indices.Peek();
greater_indices.Dequeue();
int to_remove = index;
while (--to_remove > previous_index)
{
Console.WriteLine(to_remove + " " +
index + " " + to_remove);
}
Console.WriteLine(0 + " " + index +
" " + index);
previous_index = index;
}
}
public static void canReduced( int [] arr,
int N)
{
if (arr[0] > arr[N - 1])
{
Console.WriteLine( "No" );
}
else
{
Console.WriteLine( "Yes" );
order_of_removal(arr, N);
}
}
public static void Main(String[] args)
{
int []arr = {1, 2, 3, 4};
int N = arr.Length;
canReduced(arr, N);
}
}
|
Javascript
<script>
function order_of_removal(a, n) {
var greater_indices = [];
var first = a[0];
var previous_index = 0;
for ( var i = 1; i < n; i++) {
if (a[i] > first) greater_indices.push(i);
}
while (greater_indices.length > 0) {
var index = greater_indices[0];
greater_indices.shift();
var to_remove = index;
to_remove -= 1;
while (to_remove > previous_index) {
document.write(to_remove + " " + index + " " + to_remove + "<br>" );
}
document.write(0 + " " + index + " " + index + "<br>" );
previous_index = index;
}
}
function canReduced(arr, N) {
if (arr[0] > arr[N - 1]) {
document.write( "No" + "<br>" );
}
else {
document.write( "Yes" + "<br>" );
order_of_removal(arr, N);
}
}
var arr = [1, 2, 3, 4];
var N = arr.length;
canReduced(arr, N);
</script>
|
Output: Yes
0 1 1
0 2 2
0 3 3
Time Complexity: O(N)
Auxiliary Space: O(N)