给定一个由N 个不同整数组成的数组arr[] ,任务是重新排列数组,使得没有元素与其索引相同(基于 1 的索引)。如果存在多个解决方案,请打印其中任何一个。
例子:
Input: arr[] = {4, 2, 3, 1}
Output: 3 1 4 2
Explanation: The elements at indices {1, 2, 3, 4} are {3, 1, 4, 2} respectively.
Input: arr[] = {10, 20, 30, 40, 6}
Output: 6 10 20 30 40
Explanation: The elements at indices {1, 2, 3, 4, 5} are {6, 10, 20, 30, 40} respectively.
方法:该想法是使用排序和交换每对相邻的索引的任何索引i是否ARR [i]是等于i。这是因为,如果arr[i] = i成立,那么肯定arr[i + 1] ≠ i并且arr[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
Javascript
输出:
2 1 4 5 3
时间复杂度: O(N*log N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live