Rearrange given array such that no array element is same as its index
Last Updated :
24 Feb, 2022
Given an array arr[] consisting of N distinct integers, the task is to rearrange the array such that no element is same as its index ( 1-based indexing ). If multiple solutions exist, print any one of them.
Examples:
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.
Approach: The idea is to use sorting and swap each adjacent pair of indices at any index i if arr[i] is equal to i. This is because, if arr[i] = i holds true, then definitely arr[i + 1] ? i and arr[i] ? i + 1 because arr[i + 1] > arr[i]. If the last element, arr[N] is equal to N, then swap arr[N] and arr[N – 1]. Follow the steps below to solve the problem:
- Sort the array arr[] in the increasing order.
- Traverse the array over the range [0, N – 2] using the variable i and check if arr[i] is the same as (i + 1) or not. If found to be true, then swap arr[i] and arr[i + 1].
- Now, for the last array element, if arr[N] is same as N, then swap arr[N] and arr[N – 1].
- After completing the above steps, print the modified array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rearrangeArray( int a[], int n)
{
sort(a, a + n);
for ( int i = 0; i < n - 1; i++) {
if (a[i] == i + 1) {
swap(a[i], a[i + 1]);
}
}
if (a[n - 1] == n) {
swap(a[n - 1], a[n - 2]);
}
for ( int i = 0; i < n; i++) {
cout << a[i] << " " ;
}
}
int main()
{
int arr[] = { 1, 5, 3, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
rearrangeArray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void rearrangeArray( int a[], int n)
{
Arrays.sort(a);
for ( int i = 0 ; i < n - 1 ; i++)
{
if (a[i] == i + 1 )
{
int temp = a[i];
a[i] = a[i + 1 ];
a[i + 1 ] = temp;
}
}
if (a[n - 1 ] == n)
{
int temp = a[n - 1 ];
a[n - 1 ] = a[n - 2 ];
a[n - 2 ] = temp;
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(a[i] + " " );
}
}
public static void main(String args[])
{
int arr[] = { 1 , 5 , 3 , 2 , 4 };
int N = arr.length;
rearrangeArray(arr, N);
}
}
|
Python3
def rearrangeArray(a, n):
a = sorted (a)
for i in range (n - 1 ):
if (a[i] = = i + 1 ):
a[i], a[i + 1 ] = a[i + 1 ], a[i]
if (a[n - 1 ] = = n):
a[n - 1 ], a[n - 2 ] = a[n - 2 ], a[n - 1 ]
for i in range (n):
print (a[i], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 5 , 3 , 2 , 4 ]
N = len (arr)
rearrangeArray(arr, N)
|
C#
using System;
public class GFG
{
static void rearrangeArray( int []a, int n)
{
Array.Sort(a);
for ( int i = 0; i < n - 1; i++)
{
if (a[i] == i + 1)
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
if (a[n - 1] == n)
{
int temp = a[n - 1];
a[n - 1] = a[n - 2];
a[n - 2] = temp;
}
for ( int i = 0; i < n; i++)
{
Console.Write(a[i] + " " );
}
}
public static void Main(String []args)
{
int []arr = { 1, 5, 3, 2, 4 };
int N = arr.Length;
rearrangeArray(arr, N);
}
}
|
Javascript
<script>
function rearrangeArray(a, n)
{
a.sort();
for (let i = 0; i < n - 1; i++)
{
if (a[i] == i + 1)
{
let temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
if (a[n - 1] == n)
{
let temp = a[n - 1];
a[n - 1] = a[n - 2];
a[n - 2] = temp;
}
for (let i = 0; i < n; i++)
{
document.write(a[i] + " " );
}
}
let arr = [ 1, 5, 3, 2, 4 ];
let N = arr.length;
rearrangeArray(arr, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...