📌  相关文章
📜  重新排列数组以使相似的索引元素不同于另一个数组的索引元素

📅  最后修改于: 2021-10-25 06:50:32             🧑  作者: Mango

给定两个由N 个不同整数组成的排序数组A[]B[] ,任务是重新排列数组B[]的元素,使得对于每个i索引A[i]不等于B[i] .如果存在多个这样的安排,打印其中任何一个。如果不存在这样的安排,则打印-1

例子:

朴素的方法:最简单的方法是找到数组B[] 的所有可能排列并打印它们之间的任何排列,使得对于每个i索引, A[i])不等于B[i]

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

高效方法:为了优化上述方法,其思想是使用贪婪方法通过使用两个数组由N 个不同元素按升序组成的条件来找到数组B[]所需的排列。请按照以下步骤解决问题:

  • 反转给定的数组B[]
  • 如果N1A[0] = B[0] ,则打印-1
  • 否则,遍历数组,并检查A[i] 是否等于B[i]
  • 如果A[i]等于B[i] ,则将B[i]B[i+1]交换并中断循环。
  • 完成上述步骤后,打印数组B[]

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the arrangement
// of array B[] such that element at
// each index of A[] and B[] are not equal
void RearrangeB(int A[], vector B, int n)
{
     
    // Print not possible, if arrays
    // only have single equal element
    if (n == 1 && A[0] == B[0])
    {
        cout << "-1" << endl;
        return;
    }
  
    // Reverse array B
    for(int i = 0; i < n / 2; i++)
    {
        int t = B[i];
        B[i] = B[n - i - 1];
        B[n - i - 1] = t;
    }
  
    // Traverse over arrays to check
    // if there is any index where
    // A[i] and B[i] are equal
    for(int i = 0; i < n - 1; i++)
    {
         
        // Swap B[i] with B[i - 1]
        if (B[i] == A[i])
        {
            int t = B[i + 1];
            B[i + 1] = B[i];
            B[i] = t;
  
            // Break the loop
            break;
        }
    }
  
    // Print required arrangement
    // of array B
    for(int k : B)
        cout << k << " ";
}
  
// Driver Code
int main()
{
     
    // Given arrays A[] and B[]
    int A[] = { 2, 4, 5, 8 };
    vector B = { 2, 4, 5, 8 };
     
    // Length of array A[]
    int n = sizeof(A) / sizeof(A[0]);
  
    // Function call
    RearrangeB(A, B, n);
}
 
// This code is contributed by sanjoy_62


Java
// Java program for the above approach
 
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to find the arrangement
    // of array B[] such that element at
    // each index of A[] and B[] are not equal
    static void RearrangeB(int[] A, int[] B)
    {
        // Length of array
        int n = A.length;
 
        // Print not possible, if arrays
        // only have single equal element
        if (n == 1 && A[0] == B[0]) {
            System.out.println("-1");
            return;
        }
 
        // Reverse array B
        for (int i = 0; i < n / 2; i++) {
            int t = B[i];
            B[i] = B[n - i - 1];
            B[n - i - 1] = t;
        }
 
        // Traverse over arrays to check
        // if there is any index where
        // A[i] and B[i] are equal
        for (int i = 0; i < n - 1; i++) {
 
            // Swap B[i] with B[i - 1]
            if (B[i] == A[i]) {
                int t = B[i + 1];
                B[i + 1] = B[i];
                B[i] = t;
 
                // Break the loop
                break;
            }
        }
 
        // Print required arrangement
        // of array B
        for (int k : B)
            System.out.print(k + " ");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given arrays A[] and B[]
        int[] A = { 2, 4, 5, 8 };
        int[] B = { 2, 4, 5, 8 };
 
        // Function Call
        RearrangeB(A, B);
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the arrangement
# of array B[] such that element at
# each index of A[] and B[] are not equal
def RearrangeB(A, B):
 
    # Length of array
    n = len(A)
 
    # Print not possible, if arrays
    # only have single equal element
    if (n == 1 and A[0] == B[0]):
        print(-1)
        return
 
    # Reverse array B
    for i in range(n // 2):
        t = B[i]
        B[i] = B[n - i - 1]
        B[n - i - 1] = t
 
    # Traverse over arrays to check
    # if there is any index where
    # A[i] and B[i] are equal
    for i in range(n - 1):
         
        # Swap B[i] with B[i - 1]
        if (B[i] == A[i]):
            B[i], B[i - 1] = B[i - 1], B[i]
            break
 
    # Print required arrangement
    # of array B
    for k in B:
        print(k, end = " ")
 
# Driver Code
 
# Given arrays A[] and B[]
A = [ 2, 4, 5, 8 ]
B = [ 2, 4, 5, 8 ]
 
# Function call
RearrangeB(A, B)
 
# This code is contributed by Shivam Singh


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to find the arrangement
// of array []B such that element at
// each index of []A and []B
// are not equal
static void RearrangeB(int[] A,
                       int[] B)
{
  // Length of array
  int n = A.Length;
 
  // Print not possible, if arrays
  // only have single equal element
  if (n == 1 && A[0] == B[0])
  {
    Console.WriteLine("-1");
    return;
  }
 
  // Reverse array B
  for (int i = 0; i < n / 2; i++)
  {
    int t = B[i];
    B[i] = B[n - i - 1];
    B[n - i - 1] = t;
  }
 
  // Traverse over arrays to check
  // if there is any index where
  // A[i] and B[i] are equal
  for (int i = 0; i < n - 1; i++)
  {
    // Swap B[i] with B[i - 1]
    if (B[i] == A[i])
    {
      int t = B[i + 1];
      B[i + 1] = B[i];
      B[i] = t;
 
      // Break the loop
      break;
    }
  }
 
  // Print required arrangement
  // of array B
  foreach (int k in B)
    Console.Write(k + " ");
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given arrays []A and []B
  int[] A = {2, 4, 5, 8};
  int[] B = {2, 4, 5, 8};
 
  // Function Call
  RearrangeB(A, B);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
8 5 4 2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程