📌  相关文章
📜  通过选择索引 i、j、k 并将 arr[i] 替换为 arr[j] – arr[k] 对数组进行排序

📅  最后修改于: 2022-05-13 01:56:06.930000             🧑  作者: Mango

通过选择索引 i、j、k 并将 arr[i] 替换为 arr[j] – arr[k] 对数组进行排序

给定一个包含N个整数的数组arr[] ,任务是通过将索引i ( arr[i] ) 处的任何元素替换为arr[j] – arr[k]来对数组进行排序,使得i < j < k

注意:如果不需要任何操作,打印 0。

例子:

方法:该方法基于以下事实:

按照以下步骤解决上述问题:

  • 检查最后两个元素是否已排序,因为无法对它们进行操作。
  • 如果最后一个元素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)