Java Program to Rearrange an Array such that arr[i]=i

In the previous article, we have seen
Java Program to Replace all the Positive Elements of the Array with 1 and all the Negative Elements with 0

In this article we are going to see how Rearrange an Array such that arr[i]=i using java programming language.

Java Program to Rearrange an Array such that arr[i]=i

Rearranging an array such that arr[i]=i, which means at index-0 element 0, at index-1 element 1, at index-2 element 2, at index-3 element 3 … at index-n element n like this.

Condition for this program:

  • As array index starts from 0, and we need to rearrange array such that arr[i]=i then array elements should contain all the elements from 0 to ‘n’ depending upon the array length.

Let’s see different ways to rearrange an array such that arr[i]=1.

Method-1: Java Program to Rearrange an Array such that arr[i]=i By Using Static Input Value

Approach:

  1. Iterate over the array.
  2. Check if the current element is non-negative, less than the length of the array and not in its correct position.
  3. If yes, swap the current element with the element at its correct position.
  4. Repeat steps 2 and 3 until arr[i] = i.

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String[] args) 
    {
        int[] arr = { 1, 0, 9, 5, 6, 7, 8, 2, 3, 4 };
        rearrange(arr);
    }

    public static void rearrange(int[] arr) 
    {
        System.out.println("Original array: " + Arrays.toString(arr));
        int n = arr.length;
        for (int i = 0; i < n; i++) 
        {
            while (arr[i] >= 0 && arr[i] < n && arr[i] != i) 
            {
                int temp = arr[i];
                arr[i] = arr[temp];
                arr[temp] = temp;
            }
        }
        System.out.println("Reordered array: " + Arrays.toString(arr));
    }
}

Output:

Original array: [1, 0, 9, 5, 6, 7, 8, 2, 3, 4]
Reordered array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Method-2: Java Program to Rearrange an Array such that arr[i]=i By Using User Input Value

Approach:

  1. Create scanner class object.
  2. Ask user for length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Iterate over the array.
  6. Check if the current element is non-negative, less than the length of the array and not in its correct position.
  7. If yes, swap the current element with the element at its correct position.
  8. Repeat steps 2 and 3 until arr[i] = i.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        rearrange(arr);
    }

    public static void rearrange(int[] arr) 
    {
        System.out.println("Original array: " + Arrays.toString(arr));
        int n = arr.length;
        for (int i = 0; i < n; i++) 
        {
            while (arr[i] >= 0 && arr[i] < n && arr[i] != i) 
            {
                int temp = arr[i];
                arr[i] = arr[temp];
                arr[temp] = temp;
            }
        }
        System.out.println("Reordered array: " + Arrays.toString(arr));
    }
}
Output:

Enter the size of array: 5
Enter array elements: 4 0 1 3 2
Original array: [4, 0, 1, 3, 2]
Reordered array: [0, 1, 2, 3, 4]

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: