Program to Rearrange an array such that arr[i]=i

Description

To rearrange an array such that arr[i] = i, first check if any element is 
equal to index or not, if yes swap the element.

At last, check if any element not equal to its index, replace it with -1.

C/C++

/* C Program to Rearrange an array such that arr[i] = i */
//Save it as RearrangeArray.c

#include<stdio.h>
int main(){

    int n, i, j, temp;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];

    printf("Enter the array elements : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    printf("Entered the array elements are : ");
    for(i=0;i<n;i++) {
        printf("%d ", arr[i]);
    }

    //Checking in array
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            if(arr[j] == i) {
                temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
                break;
            }
        }
    }

    //if arr[i] is not equal to i then assign it to -1
    for(i=0;i<n;i++)  {
        if(arr[i] != i) {
            arr[i] = -1;
        }
    }

    printf("\n");
    printf("Elements after rearrangement : ");
    for(i=0;i<n;i++) {
        printf("%d ",arr[i]);
    }
}
Input:
Enter the size of array : 
5
Enter the array elements : 
3
5
1
4
6

Output:
Entered the array elements are : 
3 5 1 4 6 
Elements after rearrangement : 
-1 1 -1 3 4

Java

/* Java Program to Rearrange an array such that arr[i] = i */
//Save it as RearrangeArray.java

import java.io.*;
import java.util.Scanner;

public class RearrangeArray {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int size = scanner.nextInt();
        
        int arr[] = new int[size]; 
        int i, j, temp;
        
        System.out.println("Enter the array elements : ");
        for(i=0;i<size;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Entered the array elements are : ");
        for(i=0;i<size;i++) {
            System.out.print(arr[i] + " ");
        }
        
        //Checking in array
        for(i=0;i<size;i++) {
            for(j=0;j<size;j++) {
                if(arr[j] == i) {
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                    break;
                }
            }
        }
        
        //if arr[i] is not equal to i then assign it to -1
        for(i=0;i<size;i++)  { 
            if(arr[i] != i) {
                arr[i] = -1; 
            } 
        }
        
        System.out.println();
        System.out.println("Elements after rearrangement : ");
        for(i=0;i<size;i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
Input:
Enter the size of array : 
5
Enter the array elements : 
3
5
1
4
6

Output:
Entered the array elements are : 
3 5 1 4 6 
Elements after rearrangement : 
-1 1 -1 3 4

Related Programs

1) Program for finding Second Largest Element Of Array
2) Program to form a number using entered digits
3) Program to merge two sorted arrays
4) Program to Search for an Element in an Array
5) Program to Reverse an Array using Recursion
6) Program to Find the smallest missing number
7) Program to Find missing odd number in first n odd number
8) Program to Find Value equal to index value
9) Program to Check Array is Perfect or Not
10) Program to make pair of elements alternatively
Share Me

Leave a Reply