通过选择索引 i、j、k 并将 arr[i] 替换为 arr[j] – arr[k] 对数组进行排序
给定一个包含N个整数的数组arr[] ,任务是通过将索引i ( arr[i] ) 处的任何元素替换为arr[j] – arr[k]来对数组进行排序,使得i < j < k 。
注意:如果不需要任何操作,打印 0。
例子:
Input: arr[] = {2, -2, -3, -1, 3}
Output:
3
3 4 5
2 4 5
1 4 5
Explanation: The number at 3rd position can be replaced by 4th and last position.
The array becomes {2, -2, -4, -1, 3}. So 31, 4, 5}
The number at 2nd position can be replaced by 4th and last position.
The array becomes {2, -4, -4, -1, 3}. So {2, 4, 5}
The number at 1st position can be replaced by 4th and last position.
The array becomes {-4, -4, -4, -1, 3}. So {1, 4, 5}
Array is sorted after 3 replacements, so 3 is printed at the beginning.
Input: arr[] = {1, 2, -3, -5}
Output: -1
Explanation: The array can never be sorted by the following operations.
方法:该方法基于以下事实:
The last two elements can never get effected since the need is to select any 3 indices and i < j < k. So the replacement by the difference between the last two elements makes the array sorted if the last two are sorted.
Here are the cases that come:
Case-1: If arr[N-1] >= 0 and if the array is not sorted, the elements from the index i = [0, N – 3] can be replaced by arr[N – 2] – arr[N-1] since i < j < k and the difference arr[N – 2] – arr[N-1] will be less than a[N – 2].
Case-2: If arr[N-1] < 0 it is not possible to make the array sorted by replacements.
If the last two elements arr[N – 1], arr[N-2] are sorted but a[N-1] < 0 then if some index i is chosen
arr[i] = arr[N-2] – (-a[N-1]) this becomes > a[N – 1] which violates the sorting property so NO.
Case-3: If the last two elements are not sorted then sorting is not possible because we cant modify the last two elements any way.
按照以下步骤解决上述问题:
- 检查最后两个元素是否已排序,因为无法对它们进行操作。
- 如果最后一个元素arr[N – 1]大于或等于 0,则将索引 [0, N – 3] 替换为 arr[N – 2] – arr[N-1]。
- 如果最后一个元素是不能排序的负数,如果它最初没有排序。因此,请检查数组是否已排序。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the array
// can be sorted with replacements
void is_possible(int arr[], int n)
{
// Check for the last two elements
// if they are sorted or not
// If they are not sorted print No
if (arr[n - 2] > arr[n - 1]) {
cout << "-1" << endl;
return;
}
// If last element is greater than
// or equal to 0 then elements index
// [0, n-3] can be replaced by
// a[n - 2] - a[n - 1] and it is
// possible to sort the array
if (arr[n - 1] >= 0) {
cout << n - 2 << "\n";
for (int i = 0; i <= n - 3; i++) {
cout << i + 1 << " " << n - 1
<< " " << n << endl;
}
}
// If arr[n-1] is negative,
// it not possible execept in case
// the whole array is initially
// negative sorted
else {
// Check if the array is sorted
for (int i = 0; i < n - 2; i++) {
if (arr[i] > arr[i + 1]) {
cout << "-1" << endl;
return;
}
}
// If the array is initially sorted
cout << "0" << endl;
}
}
// Driver code
int main()
{
int arr[] = { 2, -2, -3, -1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
is_possible(arr, N);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to check if the array
// can be sorted with replacements
public static void is_possible(int arr[], int n)
{
// Check for the last two elements
// if they are sorted or not
// If they are not sorted print No
if (arr[n - 2] > arr[n - 1]) {
System.out.println("-1");
return;
}
// If last element is greater than
// or equal to 0 then elements index
// [0, n-3] can be replaced by
// a[n - 2] - a[n - 1] and it is
// possible to sort the array
if (arr[n - 1] >= 0) {
System.out.println(n - 2);
for (int i = 0; i <= n - 3; i++) {
System.out.print(i + 1 + " ");
System.out.print(n - 1 + " ");
System.out.print(n);
System.out.println();
}
}
// If arr[n-1] is negative,
// it not possible execept in case
// the whole array is initially
// negative sorted
else {
// Check if the array is sorted
for (int i = 0; i < n - 2; i++) {
if (arr[i] > arr[i + 1]) {
System.out.println("-1");
return;
}
}
// If the array is initially sorted
System.out.println("0");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = new int[] { 2, -2, -3, -1, 3 };
int N = arr.length;
// Function call
is_possible(arr, N);
}
}
// This code is contributed by Taranpreet
Python
# Python program for the above approach
# Function to check if the array
# can be sorted with replacements
def is_possible(arr, n):
# Check for the last two elements
# if they are sorted or not
# If they are not sorted print No
if (arr[n - 2] > arr[n - 1]):
print(-1)
return
# If last element is greater than
# or equal to 0 then elements index
# [0, n-3] can be replaced by
# a[n - 2] - a[n - 1] and it is
# possible to sort the array
if (arr[n - 1] >= 0):
print(n - 2)
for i in range(0, n - 2):
print(i + 1, n - 1, n)
# If arr[n-1] is negative,
# it not possible execept in case
# the whole array is initially
# negative sorted
else:
# Check if the array is sorted
for i in range(0, n - 2):
if (arr[i] > arr[i + 1]):
print("-1")
return
# If the array is initially sorted
print("0")
# Driver code
if __name__ == "__main__":
arr = [ 2, -2, -3, -1, 3 ]
N = len(arr)
# Function call
is_possible(arr, N)
# This code is contributed by hrithikgarg03188.
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if the array
// can be sorted with replacements
static void is_possible(int[] arr, int n)
{
// Check for the last two elements
// if they are sorted or not
// If they are not sorted print No
if (arr[n - 2] > arr[n - 1]) {
Console.WriteLine("-1");
return;
}
// If last element is greater than
// or equal to 0 then elements index
// [0, n-3] can be replaced by
// a[n - 2] - a[n - 1] and it is
// possible to sort the array
if (arr[n - 1] >= 0) {
Console.WriteLine(n - 2);
for (int i = 0; i <= n - 3; i++) {
Console.WriteLine((i + 1) + " " + (n - 1)
+ " " + n);
}
}
// If arr[n-1] is negative,
// it not possible execept in case
// the whole array is initially
// negative sorted
else {
// Check if the array is sorted
for (int i = 0; i < n - 2; i++) {
if (arr[i] > arr[i + 1]) {
Console.WriteLine(-1);
return;
}
}
// If the array is initially sorted
Console.WriteLine("0");
}
}
// Driver code
public static void Main()
{
int[] arr = { 2, -2, -3, -1, 3 };
int N = arr.Length;
// Function call
is_possible(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
3
1 4 5
2 4 5
3 4 5
时间复杂度: O(N)
辅助空间: O(1)