📌  相关文章
📜  重新排列给定的数组,以使没有数组元素与其索引相同

📅  最后修改于: 2021-04-26 09:36:54             🧑  作者: Mango

给定一个由N个不同整数组成的数组arr [] ,任务是重新排列该数组,以使其元素与其索引(基于1的索引)相同。如果存在多个解决方案,请打印其中任何一种。

例子:

方法:如果arr [i]等于i,则可以使用排序并在任何索引i处交换每个相邻的索引对。这是因为,如果arr [i] = i成立,则肯定是arr [i + 1]≠iarr [i]≠i +1,因为arr [i + 1]> arr [i] 。如果最后一个元素arr [N]等于N ,则交换arr [N]arr [N – 1] 。请按照以下步骤解决问题:

  • 以递增顺序对数组arr []进行排序。
  • 使用变量i[0,N – 2]范围内遍历数组并检查arr [i]是否等于(i + 1) 。如果发现为真,则交换arr [i]arr [i + 1]
  • 现在,对于最后一个数组元素,如果arr [N]N相同,则交换arr [N]arr [N – 1]
  • 完成上述步骤后,打印修改后的数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to rearrange the array a[]
// such that none of the array elements
// is same as its index
void rearrangeArray(int a[], int n)
{
    // Sort the array
    sort(a, a + n);
 
    // Traverse the indices [0, N - 2]
    // of the given array
    for (int i = 0; i < n - 1; i++) {
 
        // Check if the current element
        // is equal to its index
        if (a[i] == i + 1) {
 
            // If found to be true, swap
            // current element with the
            // next element
            swap(a[i], a[i + 1]);
        }
    }
 
    // Check if the last element is
    // same as its index
    if (a[n - 1] == n) {
 
        // If found to be true, swap
        // current element with the
        // previous element
        swap(a[n - 1], a[n - 2]);
    }
 
    // Print the modified array
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 3, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    rearrangeArray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to rearrange the array a[]
// such that none of the array elements
// is same as its index
static void rearrangeArray(int a[], int n)
{
     
    // Sort the array
    Arrays.sort(a);
     
    // Traverse the indices [0, N - 2]
    // of the given array
    for(int i = 0; i < n - 1; i++)
    {
         
        // Check if the current element
        // is equal to its index
        if (a[i] == i + 1)
        {
             
            // If found to be true, swap
            // current element with the
            // next element
            int temp = a[i];
            a[i] = a[i + 1];
            a[i + 1] = temp;
        }
    }
 
    // Check if the last element is
    // same as its index
    if (a[n - 1] == n)
    {
         
        // If found to be true, swap
        // current element with the
        // previous element
        int temp = a[n - 1];
        a[n - 1] = a[n - 2];
        a[n - 2] = temp;
    }
     
    // Print the modified array
    for(int i = 0; i < n; i++)
    {
        System.out.print(a[i] + " ");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 1, 5, 3, 2, 4 };
    int N = arr.length;
     
    // Function Call
    rearrangeArray(arr, N);
}
}
 
// This code is contributed by ipg2016107


Python3
# Python3 program for the above approach
 
# Function to rearrange the array a[]
# such that none of the array elements
# is same as its index
def rearrangeArray(a, n):
   
    # Sort the array
    a = sorted(a)
 
    # Traverse the indices [0, N - 2]
    # of the given array
    for i in range(n - 1):
 
        # Check if the current element
        # is equal to its index
        if (a[i] == i + 1):
 
            # If found to be true, swap
            # current element with the
            # next element
            a[i], a[i + 1] = a[i + 1], a[i]
 
    # Check if the last element is
    # same as its index
    if (a[n - 1] == n):
 
        # If found to be true, swap
        # current element with the
        # previous element
        a[n - 1], a[n - 2] = a[n - 2], a[n - 1]
 
    # Prthe modified array
    for i in range(n):
        print(a[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 5, 3, 2, 4]
    N = len(arr)
 
    # Function Call
    rearrangeArray(arr, N)
 
    # This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
public class GFG
{
     
// Function to rearrange the array []a
// such that none of the array elements
// is same as its index
static void rearrangeArray(int []a, int n)
{
     
    // Sort the array
    Array.Sort(a);
     
    // Traverse the indices [0, N - 2]
    // of the given array
    for(int i = 0; i < n - 1; i++)
    {
         
        // Check if the current element
        // is equal to its index
        if (a[i] == i + 1)
        {
             
            // If found to be true, swap
            // current element with the
            // next element
            int temp = a[i];
            a[i] = a[i + 1];
            a[i + 1] = temp;
        }
    }
 
    // Check if the last element is
    // same as its index
    if (a[n - 1] == n)
    {
         
        // If found to be true, swap
        // current element with the
        // previous element
        int temp = a[n - 1];
        a[n - 1] = a[n - 2];
        a[n - 2] = temp;
    }
     
    // Print the modified array
    for(int i = 0; i < n; i++)
    {
        Console.Write(a[i] + " ");
    }
}
 
// Driver Code
public static void Main(String []args)
{
    int []arr = { 1, 5, 3, 2, 4 };
    int N = arr.Length;
     
    // Function Call
    rearrangeArray(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
2 1 4 5 3

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